Skip to content

Instantly share code, notes, and snippets.

@rkrzr
Forked from nickfishman/gzipRequestTest.js
Created August 8, 2013 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkrzr/6184341 to your computer and use it in GitHub Desktop.
Save rkrzr/6184341 to your computer and use it in GitHub Desktop.
var request = require('request'),
zlib = require('zlib');
var headers = {
"accept-charset" : "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"accept-language" : "en-US,en;q=0.8",
"accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"user-agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
"accept-encoding" : "gzip,deflate",
};
var options = {
url: "http://google.com",
headers: headers
};
var requestWithEncoding = function(options, callback) {
var req = request.get(options);
req.on('response', function(res) {
var chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function() {
var buffer = Buffer.concat(chunks);
var encoding = res.headers['content-encoding'];
if (encoding == 'gzip') {
zlib.gunzip(buffer, function(err, decoded) {
callback(err, decoded && decoded.toString());
});
} else if (encoding == 'deflate') {
zlib.inflate(buffer, function(err, decoded) {
callback(err, decoded && decoded.toString());
})
} else {
callback(null, buffer.toString());
}
});
});
req.on('error', function(err) {
callback(err);
});
}
requestWithEncoding(options, function(err, data) {
if (err) console.log(err);
else console.log(data);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment