如何使用BodyParser Urlenced(Postman)和BodyParser JSON(Curl)同时使用Bo



我在服务器上使用Express4:

具有此代码
// configure the app to use bodyParser()
/*app.use(bodyParser.urlencoded({
    extended: true
}));*/
app.use(bodyParser.json());

当我评论第一个集团时,我可以发送数据:

curl -i -X POST -H "Content-Type:application/json" -d '{  "firstname" : "Frodo",  "lastname" : "Baggins" }' http://localhost:3000/accounts

它运行良好,但是当我向Postman发送带有X-WWW-Form-urlenCoded的Postman和LastName Feled时,人体中的数据不起作用。

但是,如果我再次使用:

再次启用代码
app.use(bodyParser.urlencoded({
    extended: true
}));

Postman效果很好,但不能。

有两种方法一起工作的方法吗?因此我可以使用angular2而不是curl。

您可以像这样链接身体解析器:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

随后将检测到要使用的。

重要的是要注意,如果您使用

app.use(bodyParser.json({ type: '*/*' }));
app.use(bodyParser.urlencoded({ extended: true }));

身体必须像这样工作

app.use(bodyParser.json());

最新更新