Refit access body when gzip encoded

I am sending data to the body of an HTTP request to a Restify instance with gzip content encoding. The only problem is that I cannot access the body with the gzipped data in it.

Here is my JS code that I use to define it, but req.body appears 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)
}
})

Does anyone know how to access req.body here or equivalent?

+4
source share
1 answer

Latest update versions support this out of the box through the plugin bodyParser. You just need to initialize it as follows:

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

, content-encoding gzip, , req.body content-type.

+2

Source: https://habr.com/ru/post/1569741/


All Articles