Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active November 8, 2021 09:22
Show Gist options
  • Save miguelmota/9946206 to your computer and use it in GitHub Desktop.
Save miguelmota/9946206 to your computer and use it in GitHub Desktop.
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)
}
})
@1trackprojects1
Copy link

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