My question is if i make a request using the node-fetch library with header Accept-Encoding:'gzip', does node-fetch handle automatic decompression of the response data?
You don't even need to add the header yourself, it'll be added automatically by node for you.
This is easily tested by creating an index.mjs file with the following content.
const response = await fetch('https://httpbin.org/headers');
const data = await response.json();
console.log(data.headers['Accept-Encoding']);
And running the following command node index.mjs which returns br, gzip, deflate under node 20.15.0 and windows 11.
node-fetch will automatically decompress the response data if the response header contains a content-encoding attribute with a valid compression algorithm like gzip or deflate.
For requests, node-fetch adds the default header Accept-Encoding = gzip,deflate as long as the compress attribute in the options object is not manually set to false. Refer to the documentation here for further information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With