快递+邮递员,req.body为空 [英] Express + Postman, req.body is empty

查看:23
本文介绍了快递+邮递员,req.body为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经被问过很多次了,但我一直在环顾四周,仍然找不到我的问题的答案.

I know this has been asked multiple times, but I have been looking around and still can't find an answer to my problem.

这是我的代码,我确保在定义路由之前使用和配置正文解析器.我只使用 .json() 和 bodyParser 因为现在我只测试一个 POST 函数,但我什至尝试过 app.use(bodyParser.urlencoded({extended: true }));

Here is my code, I make sure to use and configure body parser before defining the routes. I'm only using .json() with bodyParser because right now I'm only testing a POST function, but I've even tried with app.use(bodyParser.urlencoded({ extended: true }));

var express = require('express'),
    bodyParser = require('body-parser'),
    app = express();

app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));

app.listen(app.get('port'), function() {
    console.log("Node app is running at localhost:" + app.get('port'))
});

app.post('/itemSearch', function(req, res) {
    //var Keywords = req.body.Keywords;
    console.log("Yoooooo");
    console.log(req.headers);
    console.log(req.body);
    res.status(200).send("yay");
});

以下是我使用 Postman 测试这条路线的方法.

Here is how I use Postman to test this route.

这是我收到的回复

Node app is running at localhost:5000
Yoooooo
{ host: 'localhost:5000',
  connection: 'keep-alive',
  'content-length': '146',
  'cache-control': 'no-cache',
  origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
  'content-type': 'multipart/form-data; boundary=----WebKitFormBoundarynJtRFnukjOQDaHgU',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
  'postman-token': '984b101b-7780-5d6e-5a24-ad2c89b492fc',
  accept: '*/*',
  'accept-encoding': 'gzip, deflate',
  'accept-language': 'en-GB,en-US;q=0.8,en;q=0.6' }
{}

在这一点上,我真的很感激任何帮助.谢谢.

At this point I would really appreciate any help. Thanks.

推荐答案

AFAIK 你需要使用 Body-Parser : https://github.com/expressjs/body-parser

AFAIK you need to use the Body-Parser : https://github.com/expressjs/body-parser

bodyParser = require('body-parser').json();
app.post('/itemSearch', bodyParser, function(req, res) {
  //var Keywords = req.body.Keywords;
  console.log("Yoooooo");
  console.log(req.headers);
  console.log(req.body);
  res.status(200).send("yay");
});

然后尝试使用 PostMan 将正文设置为 raw json:

Then try with PostMan setting the body as raw json:

{
  "test": "yay"
}

这篇关于快递+邮递员,req.body为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆