如何在Express.js中访问帖子请求的原始主体?

时间:2018-06-18 11:08:57

标签: express postman body-parser

我发邮件给你这个请求。

postman

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

{ '{"urls":["https://example.com?paramOne': 'foo',
  paramTwo: 'bar"]}' }

如何将整个身体变成这样的简单字符串?

{"urls":["https://example.com?paramOne=foo&paramTwo=bar"]}

1 个答案:

答案 0 :(得分:1)

In app.js: Replace:

app.use(express.json());

With:

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: '*/*' }));