3

I wanted to get the response body in a middleware in FastAPI. I tried the method by using the following answer on stackoverflow: https://stackoverflow.com/a/71883126/19869027 But When I use the following code in the middleware,
response_body = [section async for section in response.body_iterator]
it blocks the background tasks I use in fastapi routes, and response is not generated till background task finishes. It works fine if I remove middleware. Kindly suggest some solution. Regards

1 Answers1

1

I've encountered similar problem. Turns out it was caused by a bug in Starlette. Bumping FastAPI to version 0.87.0 fixed the issue for me. Based on a very helpful comment by r1b: https://github.com/encode/uvicorn/issues/111#issuecomment-1369272260

jarek
  • 31
  • 4