无法使用 NodeJS/ExpressJS 和 Postman 获取 POST 数据
Can't get POST data using NodeJS/ExpressJS and Postman
这是我服务器的代码:
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
属性 总是空的。
我是不是漏掉了什么?
添加请求的编码。这是一个例子
..
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
..
然后在 Postman 中 select x-www-form-urlencoded
或将 Content-Type 设置为 application/json
和 select raw
编辑 以使用 raw
原始
{
"foo": "bar"
}
Headers
Content-Type: application/json
编辑 #2 回答聊天中的问题:
- 为什么它不能与 form-data 一起使用?
当然可以,看看这个答案How to handle FormData from express 4
- 使用
x-www-form-urlencoded
和raw
有什么区别
differences in application/json and application/x-www-form-urlencoded
let express = require('express');
let app = express();
// For POST-Support
let bodyParser = require('body-parser');
let multer = require('multer');
let upload = multer();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/api/sayHello', upload.array(), (request, response) => {
let a = request.body.a;
let b = request.body.b;
let c = parseInt(a) + parseInt(b);
response.send('Result : '+c);
console.log('Result : '+c);
});
app.listen(3000);
示例 JSON 和 JSON 的结果:
设置内容类型L application/JSON:
我在使用路由器时遇到了这个问题。只有 GET 在工作,POST,PATCH 和删除反映了 "undefined" req.body。在路由器文件中使用 body-parser 后,我能够使所有 HTTP 方法正常工作...
我是这样做的:
...
const bodyParser = require('body-parser')
...
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));
...
...
// for post
router.post('/users', async (req, res) => {
const user = await new User(req.body) // here is where I was getting req.body as undefined before using body-parser
user.save().then(() => {
res.status(201).send(user)
}).catch((error) => {
res.status(400).send(error)
})
})
同样对于 PATCH 和 DELETE,用户 568109 建议的这个技巧奏效了。
我想补充的一点是,如果您通过 Express.js 生成器创建项目
在您的 app.js 中,它还会生成以下代码
app.use(express.json());
如果您将正文解析器放在此代码之上,req.body 将 return 为 null 或 undefined
你应该把它放在上面的代码下面看到正确的位置
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
这是我服务器的代码:
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
属性 总是空的。
我是不是漏掉了什么?
添加请求的编码。这是一个例子
..
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
..
然后在 Postman 中 select x-www-form-urlencoded
或将 Content-Type 设置为 application/json
和 select raw
编辑 以使用 raw
原始
{
"foo": "bar"
}
Headers
Content-Type: application/json
编辑 #2 回答聊天中的问题:
- 为什么它不能与 form-data 一起使用?
当然可以,看看这个答案How to handle FormData from express 4
- 使用
x-www-form-urlencoded
和raw
有什么区别
differences in application/json and application/x-www-form-urlencoded
let express = require('express');
let app = express();
// For POST-Support
let bodyParser = require('body-parser');
let multer = require('multer');
let upload = multer();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/api/sayHello', upload.array(), (request, response) => {
let a = request.body.a;
let b = request.body.b;
let c = parseInt(a) + parseInt(b);
response.send('Result : '+c);
console.log('Result : '+c);
});
app.listen(3000);
示例 JSON 和 JSON 的结果:
设置内容类型L application/JSON:
我在使用路由器时遇到了这个问题。只有 GET 在工作,POST,PATCH 和删除反映了 "undefined" req.body。在路由器文件中使用 body-parser 后,我能够使所有 HTTP 方法正常工作...
我是这样做的:
...
const bodyParser = require('body-parser')
...
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));
...
...
// for post
router.post('/users', async (req, res) => {
const user = await new User(req.body) // here is where I was getting req.body as undefined before using body-parser
user.save().then(() => {
res.status(201).send(user)
}).catch((error) => {
res.status(400).send(error)
})
})
同样对于 PATCH 和 DELETE,用户 568109 建议的这个技巧奏效了。
我想补充的一点是,如果您通过 Express.js 生成器创建项目 在您的 app.js 中,它还会生成以下代码
app.use(express.json());
如果您将正文解析器放在此代码之上,req.body 将 return 为 null 或 undefined 你应该把它放在上面的代码下面看到正确的位置
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());