Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created April 12, 2014 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save OliverJAsh/10538120 to your computer and use it in GitHub Desktop.
Save OliverJAsh/10538120 to your computer and use it in GitHub Desktop.
Uncompress a HTTP response when needed
var request = require('request');
var stream = require('stream');
var zlib = require('zlib');
var websiteRequest = request('http://oliverjash.me/', {
headers: {
'Accept-Encoding': 'gzip,deflate'
}
});
// Request, `http.ClientRequest` – writable stream, emits a `response` event
// containing a `http.IncomingMessage`
// Response, `http.IncomingMessage` – readable stream, emits a `readable` event
websiteRequest
.on('response', function (response) {
// Uncompress the response (if compressed to begin with) into pipe into
// a writable stream.
var contentEncoding = response.headers['content-encoding'];
var output = new stream.Writable();
var chunks = [];
output._write = function (chunk, enc, next) {
chunks.push(chunk);
next();
};
// TODO: Use `_.contains`
if (['gzip', 'deflate'].indexOf(contentEncoding) !== -1) {
response.pipe(zlib.createUnzip()).pipe(output);
} else {
response.pipe(output);
}
output
.on('finish', function () {
var responseBody = global.Buffer.concat(chunks).toString();
console.log(responseBody);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment