Last active
December 11, 2015 10:29
-
-
Save stevenklise/4587518 to your computer and use it in GitHub Desktop.
It turns out this was easy, but it was hard to find complete documentation. Here is how to make a request with request (http://npmjs.org/package/request) and unpack a gzipped body.
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'); | |
var url = "http://api.stackexchange.com/2.1/search?order=desc&sort=activity&intitle=backbone&site=stackoverflow"; | |
request({ | |
encoding: null, // this keeps the body as a buffer and not a string | |
url: url, | |
headers: { 'accept-encoding': 'gzip,deflate' } | |
}, function (err, res, body) { | |
if (err) return console.log('error', err, res, body) | |
// send the body stream to be unzipped. | |
zlib.unzip(body, function (err, buffer) { | |
if (err) return console.log('error', err) | |
// log the unzipped body to | |
console.log(buffer.toString()) | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment