Skip to content

Limit the ASGI Falcon handler scope to accept only JSON. #2435

Answered by CaselIT
0x1618 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,

You can use a middleware to check if the client accepts json, something like this:

import falcon
from falcon import Request, Response

class ExampleMiddleware:
    async def process_request(self, req: Request, resp: Response) -> None:
        if not req.client_accepts_json:
            raise falcon.HTTPNotAcceptable(
                description="This API only supports responses encoded as JSON."
            )

Regarding replies, you are the one that provides them, so they are in the format you set them.
Errors can be serialized in xml, but there is a flag since 4.0 that prevents it when set to False: https://falcon.readthedocs.io/en/stable/api/app.html#falcon.ResponseOptions.xml_error_…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@0x1618
Comment options

Answer selected by 0x1618
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants