在GZip编码时进入正文

我正在将HTTP请求正文中的数据发送到具有gzip内容编码的Restify实例。 唯一的问题是我无法访问身体与gzip的数据。

这里是我用来确定它的JS代码,但是req.body是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) } }) 

任何人都有关于如何访问这里或相当于req.body的线索?

restify的最新版本通过bodyParser插件支持这个开箱即用。 你只需要像这样初始化它:

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

请求将具有设置值gzipcontent-encoding头自动解压缩,并根据指定的content-type头parsingreq.body