Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Uncompress gzip response body in Node.js with zlib example
var request = require('request')
var zlib = require('zlib')
request(url, {encoding: null}, (err, response, body) => {
if(response.headers['content-encoding'] == 'gzip'){
zlib.gunzip(body, (err, dezipped) => {
callback(dezipped.toString())
})
} else {
callback(body)
}
})
@nemanjan00

This comment has been minimized.

Copy link

@nemanjan00 nemanjan00 commented Dec 23, 2017

Thank you a lot! <3
Saved me a lot of time.

BTW, note to anyone else reading this...

Do not be stupid as I was and do not forget encoding: null in options.

@Geczy

This comment has been minimized.

Copy link

@Geczy Geczy commented Dec 31, 2017

Request can handle it out of the box

  var request = require('request')
  request(
    { method: 'GET'
    , uri: 'http://www.google.com'
    , gzip: true
    }
  , function (error, response, body) {
      // body is the decompressed response body
      console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
      console.log('the decoded data is: ' + body)
    }
  )
@1trackprojects1

This comment has been minimized.

Copy link

@1trackprojects1 1trackprojects1 commented Oct 5, 2020

Request can handle it out of the box

  var request = require('request')
  request(
    { method: 'GET'
    , uri: 'http://www.google.com'
    , gzip: true
    }
  , function (error, response, body) {
      // body is the decompressed response body
      console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
      console.log('the decoded data is: ' + body)
    }
  )

Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.