gpt4 book ai didi

node.js - GZip 编码时访问 Restify 中的正文

转载 作者:搜寻专家 更新时间:2023-10-31 23:21:33 24 4
gpt4 key购买 nike

我正在将 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 或同等内容吗?

最佳答案

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

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

具有 content-encoding header 和值 gzip 设置的请求将被自动解压缩,并且 req.body 将根据指定的 content-type header 。

关于node.js - GZip 编码时访问 Restify 中的正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27746807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com