【问题标题】:Express req.body is empty when using the http.post使用 http.post 时表示 req.body 为空
【发布时间】:2017-11-16 12:49:48
【问题描述】:

我不知道为什么 req.body 是 EMPTY... app.js 使用 body-parser 中间件(http://www.expressjs.com.cn/4x/api.html#req.body)

var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// add baseurl
app.use('/api', index);
app.use('/api', users);

user.js

router.route('/user')
.post(function (req, res, next) {
    console.log(req.body);
    console.log(req.headers);
    //console.log(JSON.parse(Object.keys(req.body)[0]));
    res.json({title: 'RESTful: POST...'});
});

我已经打印了标题:但是 req.body 是空的,我使用 JSON.parse(Object.keys(req.body)[0]) 来解析其他类似问题的结果,但它没有为我工作。 我使用邮递员请求POST,

{ 'cache-control': 'no-cache',
'postman-token': 'db7766d7-767c-4d33-be3a-acfa18ac1d9c',
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'PostmanRuntime/6.4.1',
accept: '*/*',
host: 'localhost:3000',
'accept-encoding': 'gzip, deflate',
'content-length': '0',
connection: 'keep-alive' }

postman screenshot

【问题讨论】:

  • 你为什么需要JSON.parse?您正在传递简单的数字和字符串。
  • 您是否获得了类似的结果——或者更确切地说是失败——使用 JSON 正文发布请求?
  • @Joy 退房。
  • @Maluen 确实,我只是尝试使用它。但是抛出一个错误,然后我就被淘汰了
  • @Fissure King 如果我使用 application/json 并且原始数据是 {"name": "user"},我得到 JSON 数据表单 req.body,但是为什么不能不能通过 x-www-form-urlencoded 得到它??

标签: javascript node.js express post


【解决方案1】:

您的路线似乎配置正确。我会通过尝试使用curl 提出相同的请求来排除与邮递员的任何有趣业务。

curl -d "@/path/to/payload.json" -H "Content-Type: application/json" -X POST http://localhost:3000/api/user

然后会发生什么?

编辑:根据我在 cmets 中收集的信息,您发布的请求似乎包含 Content-Type: application/x-www-form-urlencoded 标头,但带有 JSON 正文 {"name":"user"}。换句话说,内容类型标头和正文不匹配

因为服务器期待 URL 编码的内容,它可能无法解析请求正文。因此,让Content-Type 标头与正文格式一致很重要。所以在这种情况下,你有两个选择。

  1. 保持Content-Type: application/x-www-form-urlencoded,将body改为name=user
  2. 相应地将您的标题更改为Content-Type: application/json,并将正文保持为{"name":"user"}

【讨论】:

  • 嗨,Camchéachta,如果我更改为 application/json 并且原始数据是 {"name":"user"},我可以从 req.body 获取 json 数据,但是为什么 x-www -form-urlencoded 不能??
  • { 'cache-control': 'no-cache', 'postman-token': '852df9bf-d4f2-481d-baab-f91b3354f16b', 'content-type': 'application/json', 'user-agent': 'PostmanRuntime/6.4.1', accept: '*/*',<br> host: 'localhost:3000',<br> 'accept-encoding': 'gzip, deflate', 'content-length': '19', connection: 'keep-alive' } {"name":"user"}
  • 嗨,乔伊,当然。您是否将有问题的正文作为有效的 urlencoded 字符串发送?换句话说,正文是否发送为:name=user?对于如何将正文格式化为application/x-www-form-urlencodedin this Mozilla doc,有一个很好的解释。您可能一直在发送 JSON 字符串而不是这个?
  • Hi An Camchéachta,在我的问题的页脚,有一个邮递员的截图,其中包括 application/x-www-form-urlencoded 发送的数据,我认为数据格式是正确的,因为数据结构是邮递员提供的。
  • 嗨 @An Camchéachta 保持 Content-Type: application/x-www-form-urlencoded 并将正文更改为 name=user 这种情况是我想要的,因为显示截图,但结果出乎意料。
【解决方案2】:

试试这个包。当您传递表单数据而不是 json 格式时,它很有用。

https://www.npmjs.com/package/formidable

【讨论】:

  • 嗨@parth,也许这个包是获取非json格式的表单数据的好方法。我想知道为什么当我设置了Content-Type: application/x-www-form-urlencoded,发送的数据格式是正确的,但是req收不到结果。身体?你能帮忙解释一下吗?
  • 此链接上给出的解释。 stackoverflow.com/questions/4007969/…。此外,在发送其他 json 数据时,请查看您的标头。如果它设置为 Applocation/json 然后取消选中它。
猜你喜欢
  • 2019-03-01
  • 1970-01-01
  • 2022-01-26
  • 2017-07-29
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多