Last active
November 8, 2021 09:22
-
-
Save miguelmota/9946206 to your computer and use it in GitHub Desktop.
Uncompress gzip response body in Node.js with zlib example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) |
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)
}
)
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
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.