How do I ungzip (decompress) a NodeJS request's module gzip response body?
I couldn't get request to work either, so ended up using http instead.
var http = require("http"),
zlib = require("zlib");
function getGzipped(url, callback) {
// buffer to store the streamed decompression
var buffer = [];
http.get(url, function(res) {
// pipe the response into the gunzip to decompress
var gunzip = zlib.createGunzip();
res.pipe(gunzip);
gunzip.on('data', function(data) {
// decompression chunk ready, add it to the buffer
buffer.push(data.toString())
}).on("end", function() {
// response and decompression complete, join the buffer and return
callback(null, buffer.join(""));
}).on("error", function(e) {
callback(e);
})
}).on('error', function(e) {
callback(e)
});
}
getGzipped(url, function(err, data) {
console.log(data);
});
How to decode gzip or utf-8 response in node?
This works using zlib.createGunzip()
like this:
var http = require("http"),
zlib = require("zlib");
var req = http.request(url, function (res) {
// pipe the response into the gunzip to decompress
var gunzip = zlib.createGunzip();
res.pipe(gunzip);
gunzip.on('data', function (data) {
// decompression chunk ready, add it to the buffer
buffer.push(data.toString());
}).on("end", function () {
// response and decompression complete, join the buffer and return
callback(null, buffer.join(""));
}).on("error", function (e) {
callback(e);
});
});
req.on('error', function (e) {
callback(e);
});
req.end();
Easy HTTP requests with gzip/deflate compression
Note: as of 2019, request has gzip decompression built in. You can still decompress requests manually using the below method.
You can simply combine request
and zlib
with streams.
Here is an example assuming you have a server listening on port 8000 :
var request = require('request'), zlib = require('zlib');
var headers = {
'Accept-Encoding': 'gzip'
};
request({url:'http://localhost:8000/', 'headers': headers})
.pipe(zlib.createGunzip()) // unzip
.pipe(process.stdout); // do whatever you want with the stream
Unzip POST body with node + express
Found the solution on my own, the problem was with this piece of code:
req.addListener("data", function(chunk) {
data += chunk;
});
seems that concatenating request data isn't correct, so I've switched my middleware to this:
app.use(function(req, res, next) {
var data = [];
req.addListener("data", function(chunk) {
data.push(new Buffer(chunk));
});
req.addListener("end", function() {
buffer = Buffer.concat(data);
zlib.inflate(buffer, function(err, result) {
if (!err) {
req.body = result.toString();
next();
} else {
next(err);
}
});
});
});
concatenating buffers works perfectly and I'm now able to get request body decompressed.
Gzip decompression of http Response
I have got a solution as need to add one more key to the options object as :
var options = {
url: url,
qs: params.qparams,
method: params.method,
json: params.body,
headers: {
'api_key': configkey,
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip'
},
timeout: constants.request_timeout,
encoding: null
};
If someone has a better approach to perform the decompression, please add-on.
How to decompress binary data in node js
Node.js has built-in ZLib support. Once you are done decrypting, you can use:
zlib.unzip
orzlib.unzipSync
for unzippinginflate
orinflateSync
for Inflating
Hope that helps!
Related Topics
Difference Between "Process.Stdout.Write" and "Console.Log" in Node.Js
How to Access Google Sheet Spreadsheets Only with JavaScript
Paste an Image from Clipboard Using JavaScript
Getting a Unhandledpromiserejectionwarning When Testing Using Mocha/Chai
Preventing Click Event with Jquery Drag and Drop
What Is Array Literal Notation in JavaScript and When Should You Use It
Compare 2 Arrays Which Returns Difference
Cross Domain Post Request Is Not Sending Cookie Ajax Jquery
Does a JavaScript If Statement with Multiple Conditions Test All of Them
Deep Copy an Array in Angular 2 + Typescript
How to Send a Message to a Particular Client with Socket.Io
How to Debug JavaScript with Ie 8
How to Create a Hash or Dictionary Object in JavaScript
Jquery - Create Hidden Form Element on the Fly
How to Populate the Options of a Select Element in JavaScript
How to Get a Specific Parameter from Location.Search
Stop Cursor from Jumping to End of Input Field in JavaScript Replace