4

I am sending data in the body of a HTTP request to a Restify instance with a content-encoding of gzip. Only problem is I am unable to access the body with the gzipped data in it.

Here is my JS code that I am using to determine it, but req.body comes out as undefined:

server.use(function(req, res, next) {
if(req.headers['content-encoding'] == 'gzip')
{
    console.log(req);

    // We have a gzipped body here
    var zlib = require("zlib");
    var gunzip = zlib.createGunzip();

    console.log("Body: "+ req.body)
    console.log("Length: " + req.body.length);
}
else
{
    apiKey = req.query['apiKey'];

    authenticateAndRoute(apiKey, req, res, next)
}
})

Anybody have a clue on how to access req.body here or an equivalent?

Khirok
  • 517
  • 1
  • 13
  • 21

1 Answers1

2

Latest versions of restify support this out of the box via the bodyParser plugin. You just need to initialise it like this:

server.use(restify.bodyParser({
  bodyReader: true
}));

Request that have the content-encoding header with value gzip set will be automatically unzipped and the req.body will be parsed according to the indicated content-type header.

Vlad Stirbu
  • 1,732
  • 2
  • 16
  • 27