简体   繁体   English

由CURL发送的发布请求被正确解析,但是从邮递员发送时,我得到未定义的数据

[英]Post request sent by CURL gets parsed correctly but when sent from postman, I get undefined data

I am learning Node.js as a result I am setting up a authentication service. 我正在学习Node.js,因此正在设置身份验证服务。 I have an issue parsing the body from post request. 我在从请求中解析正文时遇到问题。

This is my index.js file 这是我的index.js文件

const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const db = require('./queries')
const port = 3000
app.use(express.json());
app.use(bodyParser.json())
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
)
app.get('/', (request, response) => {
  response.json({ info: 'Node.js, Express, and Postgres API' })
})

app.post('/login',function(req,res){
  var username=req.body.username;
  var password=req.body.password;
  console.log("User name = "+username+", password is "+password);
  res.end("yes");
});

This is printed on the console: 这打印在控制台上:

bash-3.2$ node index.js 
App running on port 3000.
Username = undefined, password is undefined

But when i use CURL 但是当我使用CURL时

curl --data "username=Jerry&password=jerry@example.com" http://localhost:3000/login

It works. 有用。 Don't know why? 不知道为什么吗

You have to change the postman header settings. 您必须更改邮递员头设置。 Try changing the header content type value to application/json and change body to raw. 尝试将标题内容类型的值更改为application / json并将主体更改为raw。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM