Request: how to unzip gzip response

Created on 27 Dec 2013  ·  6Comments  ·  Source: request/request

Example:

function response (err, response, body) {

// current how to unzip body ?

}
request.get({
    url : path,
    headers: {
        'Accept-Encoding' : 'gzip'
    }
}, response);

Most helpful comment

@VRMink here it is example

  zlib = require('zlib');

function response (err, res, body) {
//check res header it is gzip
 console.log(res.headers['content-encoding'])
//now body it is gzip stream buffer
      zlib.unzip(body, function(err, buffer) {
             console.log(buffer.toString())
       }

}
    request.get({
        url : apiUrl,
        headers: {
            'Accept-Encoding' : 'gzip',
          },
        encoding : null  // it is very import!!
    }, response);



All 6 comments

I resolve it.

How did you resolve it?

@VRMink here it is example

  zlib = require('zlib');

function response (err, res, body) {
//check res header it is gzip
 console.log(res.headers['content-encoding'])
//now body it is gzip stream buffer
      zlib.unzip(body, function(err, buffer) {
             console.log(buffer.toString())
       }

}
    request.get({
        url : apiUrl,
        headers: {
            'Accept-Encoding' : 'gzip',
          },
        encoding : null  // it is very import!!
    }, response);



Thanks - that was not very obvious :)

@youxiachai Thanks!

Thanks for your solution!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sherodtaylor picture sherodtaylor  ·  18Comments

reconbot picture reconbot  ·  86Comments

mikeal picture mikeal  ·  352Comments

joe-spanning picture joe-spanning  ·  29Comments

konsumer picture konsumer  ·  18Comments