【问题标题】:Retrieve JSON POST Parameters in express node在 express 节点中检索 JSON POST 参数
【发布时间】:2019-05-10 02:31:48
【问题描述】:

这是我第一次使用 node js。我正在通过 Postman 发送邮寄请求。问题是我无法获得这些参数。它说未定义或为空

这是我迄今为止尝试过的

var express = require("express");
var app = express();


var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
    extended: true
}));


// support encoded bodies
app.listen(3000, () => {
    console.log("Server running on port 3000");
});






app.post('/login',function(req,res){

    console.log('request =' + JSON.stringify(req.body))
    console.log(req.body);

    var user_name=req.body.user;
    var password=req.body.password;
    res.send(req.body);

    console.log("User name = "+user_name+", password is "+password);
    res.end("yes");
});

express 版本是 4.16.4

这是显示在控制台上的内容

【问题讨论】:

  • 你能添加你的控制台日志吗
  • 您是否将 'application/json' 作为内容类型标头发送?
  • 你用的是什么版本的 Postman,好像很老了。
  • @NirLevy 我已经更新了我的问题
  • @BraveButter 我在我的问题中添加了控制台日志

标签: node.js express postman postman-pre-request-script


【解决方案1】:

在请求中设置标题:{"Content-Type": "application/json"}。改变它会解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 2021-11-27
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多