-
Hello, as the title says. How do I limit the ASGI Falcon handler scope to accept only JSON from incoming traffic and respond only with JSON? |
Beta Was this translation helpful? Give feedback.
Answered by
CaselIT
Feb 24, 2025
Replies: 1 comment 1 reply
-
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. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
0x1618
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
You can use a middleware to check if the client accepts json, something like this:
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_…