Pipe gzipped content without decompression to response in NodeJS

Viewed 305

I am using the node-fetch library and it by-default decompresses the response. I want to make a POST request which returns gzipped content and I want to pipe the POST request response to the response.

The code I am currently using is here:

router.post('/getData', async(request, response) => {
fetch(`http://url:port/path`, { method: 'POST', headers: {'Accept-Encoding': 'gzip'}, body: ''})
        .then(data=> {
             return data.body.pipe(response);
         }
    }
}

I understand that the node-fetch library decompresses the data by default. I do not need this. I want to pass the compressed data directly using streams. (Like a proxy)

1 Answers

What worked for me was setting compress=false but then adding the header to accept gzip encoding:

fetch(url, {
    compress: false,
    headers: { "accept-encoding": "gzip" },
});
Related