这是我的服务器的代码:
var express = require('express');
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
app.post("/", function(req, res) {
res.send(req.body);
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
从 Postman,我向 http://localhost:3000/ 发起 POST 请求,在 Body/form-data 中,我有一个键“foo”和值“bar”。
但是,我在响应中不断收到一个空对象。 req.body
属性始终为空。
原文由 user2923322 发布,翻译遵循 CC BY-SA 4.0 许可协议
添加请求的编码。这是一个例子
然后在 Postman 中选择
x-www-form-urlencoded
或将 Content-Type 设置为application/json
并选择raw
编辑 以使用 raw
生的
标头
编辑#2 回答聊天中的问题:
你当然可以,看看这个答案 How to handle FormData from express 4
x-www-form-urlencoded
和raw
有什么区别application/json 和 application/x-www-form-urlencoded 的区别