Skip to content

Instantly share code, notes, and snippets.

@stevenklise
Last active December 11, 2015 10:29
Show Gist options
  • Save stevenklise/4587518 to your computer and use it in GitHub Desktop.
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.
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