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

How do I convert result from opening gzip file to string

var request = require('request')
{ method: 'GET'
, uri: ''
, 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

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


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');

  method   : 'GET',
  uri      : '',
  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