Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node-fetch automatic gzip decompression

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?

like image 880
Muhammad Ali Nazar Avatar asked Aug 11 '26 10:08

Muhammad Ali Nazar


2 Answers

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.

like image 75
baywet Avatar answered Aug 13 '26 23:08

baywet


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.

like image 34
Archulan Rajakumaran Avatar answered Aug 14 '26 00:08

Archulan Rajakumaran