I got this snippet from here.
When implemented, response = await call_next(request)
gets stuck indefinitely when the JSON payload is too large.
What could be the reason? Is it the right way to modify the body of JSON in FastAPI?
class RequestContextLogMiddleware(BaseHTTPMiddleware):
async def set_body(self, request: Request):
receive_ = await request._receive()
async def receive() -> Message:
return receive_
request._receive = receive
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):
await self.set_body(request)
body = await request.body()
jsonbody = await request.json()
id_ = jsonbody['external_id']
response = await call_next(request)
return response