Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force webserver to return uncompressed data (No gzip) [duplicate]

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.

like image 524
Sachin Avatar asked Oct 21 '22 19:10

Sachin


1 Answers

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;
     }
  });
});
like image 115
danypype Avatar answered Oct 27 '22 09:10

danypype