如何在Express.js中访问发布请求的原始正文? [英] How to access raw body of a post request in Express.js?

查看:116
本文介绍了如何在Express.js中访问发布请求的原始正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过邮递员发送了此请求。

I send this request with postman.

console.log(req.body)返回这样的数组:

{ '{"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&paramTwo=bar"]}


推荐答案

app.js 中:
替换:

In app.js: Replace:

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中访问发布请求的原始正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆