Created
April 12, 2014 14:21
-
-
Save OliverJAsh/10538120 to your computer and use it in GitHub Desktop.
Uncompress a HTTP response when needed
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 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