Skip to content

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

Description

@derf974

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,

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions