Route Handlers - Dealing with GZIP Body
Unanswered
Brown bear posted this in #help-forum
Brown bearOP
Hey, I have a client that's making requests to me with
Content-Encoding: gzip. The body from await req.arrayBuffer() starts with 1f 8b (the gzip header) and await req.json() is nonsense. How am I supposed to work with gzip encoded bodies?37 Replies
so try putting req.body directly into a gzip library that accepts
ReadableStreamBrown bearOP
So, next doesn't support requests in GZIP encoding? You're supposed to do that part by hand?
Should I set up a middleware to do this for me?
it's HTTP
Request objit doesn't have a method for gzip stuff so that's on your own
also, you can get the result as a blob and change it that way
@!=tgt it doesn't have a method for gzip stuff so that's on your own
you'd also need a library anyways for gzip
Brown bearOP
What in the world
so for every single one of my endpoints that allows
Content-Encoding: gzip, I need to call some "get gzip encoded body" method 👀with req.blob()
Brown bearOP
Wow
Is gzip encoded requests not very common?
I just expected this to be magic
@Brown bear Is gzip encoded requests not very common?
not as i heard of
Brown bearOP
I guess I have one last question
Is there a way I can pass data from the middleware to a Route Handler
Like an attribute or something
Setting a property on the NextRequest doesn’t seem to work
you could do
and get the uncompressed blob
const blob = await req.blob()
const ds = new DecompressionStream("gzip");
const decompressedBlob = blob.stream().pipeThrough(ds);and get the uncompressed blob
@Brown bear Is there a way I can pass data from the middleware to a Route Handler
what are you trying to do
Brown bearOP
Well at first I was trying to set body to that decompressed ReadableStream
you don't use middleware for that
Brown bearOP
Then when I found out body was read only I tried putting that ReadableStream somewhere else
that wouldn't make any sense
Brown bearOP
But yeah I’m curious if this is possible
My Auth middle ware gets a User object when it succeeds
And it’s important to make that accessible to the Route Handler
So I’m wondering if there’s no feature to attach an object to a request, how does this information make it to the route handler
Brown bearOP
getServerSideProps?@!=tgt that wouldn't make any sense
Brown bearOP
also, all I'm saying is that it doesn't make sense to repeat this logic in every method if every method is going to accept this encoding 😭
middleware makes sense to enable this feature across the server