如何在 Express.js 中访问 post 请求的原始正文? [英] How to access raw body of a post request in Express.js?
本文介绍了如何在 Express.js 中访问 post 请求的原始正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过邮递员发送此请求.
I send this request with postman.
而我 console.log(req.body)
返回一个这样的数组:
And I console.log(req.body)
returns an array like this:
{ '{"urls":["https://example.com?paramOne': 'foo',
paramTwo: 'bar"]}' }
我怎样才能把整个身体变成这样一个简单的字符串?
How can I get the whole body as a simple string like this?
{"urls":["https://example.com?paramOne=foo¶mTwo=bar"]}
推荐答案
在 app.js
中:替换:
app.use(express.json());
与:
var rawBodySaver = function (req, res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8');
}
}
app.use(bodyParser.json({ verify: rawBodySaver }));
app.use(bodyParser.urlencoded({ verify: rawBodySaver, extended: true }));
app.use(bodyParser.raw({ verify: rawBodySaver, type: '*/*' }));
这篇关于如何在 Express.js 中访问 post 请求的原始正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文