问题
I send this request with postman.
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"]}
回答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: '*/*' }));
来源:https://stackoverflow.com/questions/50908120/how-to-access-raw-body-of-a-post-request-in-express-js