GZipMiddleware (unzipping encoded requests)
See original GitHub issueDear maintainers,
I am looking for the appropriate way to unzip clients requests which are gzip encoded. Looking at the GZipMiddleware
it seems that it only zip responses but do not care about requests.
Thus I tried to do it at the dispatch level without success. I did something like:
@app.middleware("http")
async def dispatch(request, call_next):
plain_body = gzip.decompress(request.body())
request._body = plain_body # I don't think it is the appropriate way
response = await call_next(request) # hangs here
return response
I am not extremely familiar with the inner-working of starlette so I don’t know how to “push back in the queue” the decoded the request with the decoded body. If found no middlewares doing similar things.
Anyone can pinpoint me the appropriate way to do it ?
Thank you in advance for your help 😉
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:14 (10 by maintainers)
Top Results From Across the Web
Re: Proposal: add request decompression to gzip middleware
The idea behind this is simply to decompress the body of requests containing Content-Encoding: gzip header. I provided a working example on this...
Read more >Accept gzipped body in FastAPI / Uvicorn - Stack Overflow
So far the following: added the GZipMiddleware, but it encodes responses, rather that decoding requests; added a 'Content-Encoding: gzip' to my ...
Read more >Request decompression in ASP.NET Core - Microsoft Learn
Uses the Content-Encoding HTTP header to automatically identify and decompress requests which contain compressed content.
Read more >Gzip Middleware recipe for FastAPI - DEV Community 👩💻👨💻
_receive() if "gzip" in request.headers.getlist("Content-Encoding"): print(receive_) data = gzip.decompress(receive_.get('body')) ...
Read more >Python django.middleware.gzip.GZipMiddleware() Examples
assertEqual( self.decompress(b''.join(r)), b''.join(x.encode() for x in ... request = self.rf.get('/', HTTP_ACCEPT_ENCODING='gzip, deflate') response ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We’re not preventing anyone from developing a GZipMiddleware that handles unzipping requests. You’re welcome to do so as a third party package, and we’d happily link to it from the docs.
Above gzip decompression code in an example: https://github.com/romanmar/starlette-gzip-router/blob/master/gzip_router.py