How to Ungzip (Decompress) a Nodejs Request's Module Gzip Response Body

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 or zlib.unzipSync for unzipping
  • inflate or inflateSync for Inflating

Hope that helps!



Related Topics



Leave a reply



Submit