如何使用JSON数据获取API发布请求

时间:2019-03-26 16:59:18

标签: node.js api post request

我正尝试使用邮递员和正文原始数据提出API请求,但是我无法在服务器端读取数据。

我的要求: http://prntscr.com/n38hes

http://prntscr.com/n38hq9

我已经尝试过:

app.post('/test', function (req, res) {
    console.log(req.query)
  })

和:

app.post('/test', function (req, res) {
    console.log(req.body)
  })

但它们两个都打印{}

我想获得我使用邮递员发出的请求的用户名和密码。

1 个答案:

答案 0 :(得分:3)

确保已配置正文解析器。

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

    //here app is express app
    app.use(bodyParser.urlencoded({
      extended: true
    }));
    app.use(bodyParser.json());