Keitaro Urashima Keitaro Urashima - 1 year ago 135
Node.js Question

How do I convert result from opening gzip file to string

var request = require('request')
request(
{ method: 'GET'
, uri: 'http://www.examplewebsite.com'
, gzip: true
}
, function (error, response, body) {
console.log(body); //I am trying print body here
console.log(response.body); // //I am trying print body here too
}
)


All I received is just
�V*.I,)-V�*)*M�QJI,IT��V��,.���%�E��)JV�����d��$1

If I try to use JSON.stringify(body), the result is:

\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003�V*.I,)-V�*)*M�QJI,IT��V��,.\u0001��\u0019�\u0005%�E��)JV�����\u0000d��$1\u0000\u0000\u0000


All I want is to see the plain string. How do I do that?

Answer Source

I would expect that setting gzip : true would decompress the response body automatically, but perhaps the server doesn't set the correct Content-Encoding header.

In that case, you can try this:

const zlib    = require('zlib');
const request = require('request');

request({
  method   : 'GET',
  uri      : 'http://www.examplewebsite.com',
  gzip     : true,
  encoding : null,
}, function (error, response, body) {
  let decompressed = zlib.gunzipSync(body).toString();
  ...
})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download