Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware returns only plain text response, should support JSON or respect the Accept header. #22

Open
derf974 opened this issue Sep 27, 2024 · 1 comment

Comments

@derf974
Copy link

derf974 commented Sep 27, 2024

Hello,

I've noticed that when the middleware returns a 403 Forbidden response, it only returns a plain text response:

"Forbidden"

However, in many cases, it would be preferable if the response was in JSON format or, at the very least, if the middleware respected the Accept header provided by the client. This would allow clients to specify the desired response format.

For example, the response could look like this when returning JSON:

{
  "detail": "Forbidden"
}

I believe this would enhance the flexibility of the middleware, especially when working with modern APIs that commonly expect JSON responses.

Here's the current implementation of the middleware:
https://github.com/pycasbin/fastapi-authz/blob/master/fastapi_authz/middleware.py
image

Workaround
In my case, I have used the following workaround to return a JSON response instead of plain text:
image

And this workaround returns the following JSON structure:

{
  "detail": "Forbidden"
}

I think this feature could be valuable to many users. I'd be happy to contribute by submitting a pull request if this improvement aligns with the project's direction.

Let me know what you think!

Best regards,

@hsluoyz
Copy link
Member

hsluoyz commented Sep 27, 2024

@derf974 can you make a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants