没有语法错误时Postman错误请求错误

问题描述 投票:0回答:4

我一直在尝试向 Postman 上的服务器发送发布请求。但我一直收到 400 Bad Request 错误。错误描述为“由于语法错误,无法满足请求”。但是,我的 JSON 主体没有任何错误的语法,其他类似的问题没有涵盖语法正确时发生的此错误。

这就是标题的样子:

发生这种情况的原因可能是什么?

编辑: 这是服务器端的代码:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const articleRoutes = express.Router();
const PORT = 4000;

let Article = require('./article.model');

app.use(cors());
app.use(bodyParser.json());

mongoose.connect('mongodb://127.0.0.1:27017/articles', { useNewUrlParser: true, useUnifiedTopology: true });
const connection = mongoose.connection;

connection.once('open', function() {
    console.log("MongoDB database connection established successfully");
});

articleRoutes.route("/content-management-system/add").post((req, res) => {
    let article = new Article(req.body);
    console.log(article);
    article.save()
        .then(article => {
            res.status(200).json({'article': 'article saved successfully'});
        })
        .catch(err => {
            res.status(400).send('adding new article failed');
        });
});

app.use('/', articleRoutes);

app.listen(PORT, function() {
    console.log("Server is running on Port: " + PORT);
});
json rest express postman http-status-code-400
4个回答
1
投票

我从某处复制了正文。在复制粘贴过程中,文本中添加了诸如空格之类的内容。这导致了 400 错误。我“美化”了我的文本并解决了问题。


0
投票

问题出在我的 mongoose.connect 行中,我在

articles
之后应该是
article
的地方放置了
mongodb://127.0.0.1:27017/


0
投票

ensure that you have all these marked as indicated in the screenshot this saved me


-1
投票
  • 有时这可能是一些愚蠢的错误,比如在 应该是一个帖子请求。
  • 其他时候,正文中的语法可能不正确。
  • 或者如上所述,端点拼写错误。
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.