-
-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Caching of request body for passing down to middlewares #2387
Fix: Caching of request body for passing down to middlewares #2387
Conversation
This has been discussed extensively. Putting the body in state breaks things in multiple ways. Consider for example a middleware that modifies the request body, e.g. unzips it. If a middleware upstream put it in state then the request handler won't see the unzipped body, it will see the original body. That said, I'm not sure what your actual use case is but there are safe ways to access the request body in a middleware and in the downstream app. Take a look at starlette/starlette/middleware/base.py Lines 19 to 91 in 483849a
If you're writing your own middleware I'd recommend you read this section of the docs. At the very least it will give you a better understanding of what's going on under the hood: https://www.starlette.io/middleware/#pure-asgi-middleware |
8fdb171
to
1b0ceb4
Compare
|
@adriangb Also, about the issue with Gzip, I guess it will break, if the we place the cache middleware before the Gzip middleware. Creating a separate middleware would be better way to do this I guess, with an instructions to always keep it at the lowest stream. |
I don't think we should move forward with this or similar changes. Please look at the links in my previous comment and see if you can solve the problem that way or report back why that won't work. |
As Adrian said, this was discussed extensively. Also, you solved your use case, so let's close this. If you need more guidance, please open a discussion. |
Summary
Hey all, I was trying to figure out a way to get the request body inside a middleware that I need for logging the audit logs. But seems like, we don't have that much flexibility for getting the
multipart/form-data
#495 .I tried to make it work by caching the data in the
state
itself. I don't know whether it is the best way to fix this thing. I'm quite new to this community, please assist me for fixing this issue.Thanks
Checklist