简体   繁体   English

来自 postman 的发布请求似乎没有发送任何 json 数据

[英]Post request from postman seems to not be sending any json data

I am trying to test my project out, and I am using postman right now to pass in some data: I have set postman to "POST" to my local server through "body", and then I send the following:我正在尝试测试我的项目,我现在正在使用 postman 传递一些数据:我已将 postman 设置为“POST”到我的本地服务器通过“body”,然后我发送以下内容:

{
    "name": "My Name"
}

Then, my server file post request is:然后,我的服务器文件发布请求是:

const express = require('express'),
router        = express.Router(),
gravatar      = require('gravatar'),
bcrypt        = require('bcryptjs'),
{check, validationResult} = require('express-validator');

// User Model
const User = require('../../models/User');

// @route       GET api/users
// @description Test Route
// @access      Public
router.get('/', (req, res) => {
    res.send("User Route")
});

// @route       POST api/users
// @description Register User
// @access      Public
router.post('/', [
    check('name', 'Name is required').not().isEmpty(),
    check('email', 'Please enter a valid email address').isEmail(),
    check('password', 'Password must contain at least 6 characters').isLength({min: 6})
], async (req, res) => {
    return res.send(req.body);
});

module.exports = router;

I put in the res.send(req.body) to confirm anything is being sent to my server in general before I test any of the other code out, however, the res.send only returns: {} meaning that no json information has been sent to my server.在我测试任何其他代码之前,我输入 res.send(req.body) 以确认任何内容都被发送到我的服务器,但是,res.send 仅返回:{} 表示没有 json 信息已发送到我的服务器。 I know that the routes work because when I put in a GET request, it acts as expected.我知道路由有效,因为当我输入 GET 请求时,它会按预期运行。 Is there something obvious I am missing here?我在这里有什么明显的遗漏吗? Thank you.谢谢你。

在此处输入图像描述

I have figured out the answer to my issue.我已经找到了我的问题的答案。 In the "Headers" I was missing the "Content-type - application/json".在“标题”中,我缺少“内容类型 - 应用程序/json”。 After adding this is seems to work.添加后这似乎有效。

在此处输入图像描述

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

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