5

I am using http node.js module to make http requests. I want to force the webserver to return uncompressed data. [No gzip, No deflate].

Request headers

headers: {
  'Accept-Encoding': 'gzip,deflate,sdch',
  'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/31.0.1650.57 Chrome/31.0.1650.57 Safari/537.36',
}

I tried using this 'Accept-Encoding': '*;q=1,gzip=0' but no luck.

I see two approaches:

  1. Force the webserver to return uncompressed data.
  2. Unzip the compressed data using some nodeJs module

I want to go for #1.

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168

1 Answers1

3

If you are making http requests to an external server you can't control, and it doesn't react to the Accept-Encoding request header, then you must handle the compressed response and decompress it later. I suggest you using the zlib module. Here's an example:

var zlib = require('zlib');

//...

request.on('response', function(response){
  var contentEncoding = response.headers['content-encoding'];

  response.on('data', function(data){
    switch(contentEncoding){
      case 'gzip':
        zlib.gunzip(data, function(error, body){
          if(error){
            //Handle error
          } else {
            //Handle decompressed response body
          }
        });
        break;
      case 'deflate':
        zlib.inflate(data, function(error, body){
          if(error){
            //Handle error
          } else {
            //Handle decompressed response body
          }
        });
        break;
      default:
        //Handle response body
        break;
     }
  });
});
danypype
  • 443
  • 4
  • 10