【问题标题】:Post params not able to grab from postman + node js无法从邮递员 + 节点 js 中获取帖子参数
【发布时间】:2017-05-08 01:59:13
【问题描述】:

我的路由文件中有以下代码:

router.post('/submit', function(req, res) {
    var email = req.body.email;
    console.log(email);
});

我正在从邮递员那里拨打电话,详细信息如下: http://localhost:3000/login/submit

参数:

email=abcxyz@gmail.com 

和标题我都试过了

Content-Type:application/json 

Content-Type: application/x-www-form-urlencoded

另外,我在 app.js 中单独安装了正文解析器

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

但是,电子邮件的控制台日志显示“未定义”。为什么我无法使用 req.body.email 获取帖子参数。

【问题讨论】:

  • 在body选项中你选择什么?表单数据、x-www-form-urlencoded、原始还是二进制?
  • 你可以通过这个调用localhost:3000/login/submit来访问你的api吗?
  • 您正在节点应用程序中为“/submit”创建一个发布路线,并尝试使用邮递员点击localhost:3000/login/submit。这是正确的吗?

标签: node.js rest express postman


【解决方案1】:
    [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);

Please see the below example in the link
https://stackoverflow.com/questions/41955103/cant-get-post-data-using-nodejs-expressjs-and-postman/53514520#53514520

This will help you][1]

Set Body

Set Content-type

【讨论】:

  • 好的。我将添加答案。谢谢
  • 现在好了?
【解决方案2】:

我遇到了类似的问题并得到了它的工作,请按照以下步骤操作

  1. 在“标题”选项卡中输入如下内容类型

内容类型:application/x-www-form-urlencoded

  1. 在 Body 选项卡中选择 x-www-form-urlencoded 复选框并输入参数。

email=abcxyz@gmail.com

现在在您的节点应用程序中使用以下代码

router.post('/submit', function(req, res) {
    var email = req.body.email;
    console.log(email);
});

【讨论】:

    【解决方案3】:

    就我而言

    http://127.0.0.1:3000/submit

    router.post('/submit', function(req, res) {
    console.log("Your Email is "+req.body.email);
    res.end(); // if you not end the response it will hanging...
    

    仅设置标题类型

    Content-Type application/x-www-form-urlencoded
    

    身体

    控制台输出

    【讨论】:

      猜你喜欢
      • 2018-10-12
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 2021-06-14
      相关资源
      最近更新 更多