【问题标题】:Access Body in Restify when GZip Encoded在 GZip 编码时访问 Restify 中的正文
【发布时间】:2015-03-01 01:25:24
【问题描述】:

我正在将 HTTP 请求正文中的数据发送到内容编码为 gzip 的 Restify 实例。唯一的问题是我无法访问包含压缩数据的正文。

这是我用来确定它的 JS 代码,但 req.body 显示为未定义:

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 或等效的吗?

【问题讨论】:

标签: node.js http http-headers restify


【解决方案1】:

最新版本的 restify 通过 bodyParser 插件支持这一点。你只需要像这样初始化它:

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

具有content-encoding 标头和值gzip 的请求将被自动解压缩,req.body 将根据指示的content-type 标头进行解析。

【讨论】:

    猜你喜欢
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多