Skip to content

Instantly share code, notes, and snippets.

@mscdex
Forked from OliverJAsh/foo.js
Last active August 29, 2015 13:59
Show Gist options
  • Save mscdex/10543907 to your computer and use it in GitHub Desktop.
Save mscdex/10543907 to your computer and use it in GitHub Desktop.
var request = require('request');
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 = response;
// TODO: Use `_.contains`
if (['gzip', 'deflate'].indexOf(contentEncoding) !== -1) {
output = response.pipe(zlib.createUnzip());
}
var responseBody = '';
output.setEncoding('utf8');
output
.on('data', function (data) {
responseBody += data;
})
.on('end', function () {
console.log(responseBody);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment