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)
}
})
@nemanjan00
Copy link

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
Copy link

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
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