Node-fetch: Can I force gzip decode?

Created on 14 May 2020  路  5Comments  路  Source: node-fetch/node-fetch

My request returns a gzip encoded response but the responding server does not set the content-encoding to gzip. Looking at the code, the automatic gzip decoding only activates if content-encoding is gzip or am I missing something. Is there a way to force enable the gzip decoding? Or am i stuck with having to decode the response with my own code?

Most helpful comment

@teolag That's right - when there is no `content-encoding header, node-fetch does not decode the response:

https://github.com/node-fetch/node-fetch/blob/0936a9af0a049eb05897779f3f0df6b903a95d60/src/index.js#L225

Right now there is no other way than handling the decompression yourself. I would highly recommend asking the server owner (if it's possible of course) to add this header. We might add an option to force the decoding in the future.

All 5 comments

gzip is backend related, you have to send back headers to your front end. You can include however the content header but that's not a guarantee that it forces your server to gzip or encode the response if you have are not doing it properly. Just activate gzip in your web server.

The response is encoded, but the server I'm connecting to has forgotten to set the content-encoding header, so node-fetch don't believe the response is decoded. The server is out of my control otherwise I would just have enabled the header. Therefor I want a way to force node-fetch to decode the content, or trick it to believe it is encoded.

In that case you have to use a gzip JS implementation to help you decompress the response

Closing, because the issue is not related to node-fetch

@teolag That's right - when there is no `content-encoding header, node-fetch does not decode the response:

https://github.com/node-fetch/node-fetch/blob/0936a9af0a049eb05897779f3f0df6b903a95d60/src/index.js#L225

Right now there is no other way than handling the decompression yourself. I would highly recommend asking the server owner (if it's possible of course) to add this header. We might add an option to force the decoding in the future.

Was this page helpful?
0 / 5 - 0 ratings