-1

I have been trying to send a post request to my server on Postman. But I have been getting the 400 Bad Request Error. The description of the error says "The request cannot be fulfilled due to bad syntax." However, my JSON body does not have any incorrect syntax, and other similar questions do not cover this error occurring when the syntax is correct.

enter image description here

This is what the headers look like:

enter image description here

What could possibly be the reason behind this happening?

Edit: This is the code of the server side:

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);
});
Stacking_Flow
  • 115
  • 1
  • 1
  • 11

3 Answers3

0

The issue was in my mongoose.connect line where I put articles when it should be article after mongodb://127.0.0.1:27017/.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Stacking_Flow
  • 115
  • 1
  • 1
  • 11
  • An incorrect endpoint is still invalid syntax even if it's not in the body. Postman's '400 Bad Request' error simply states "_The request cannot be fulfilled to the bad syntax_". It does not state anything about the bad syntax being in the body of the request. – TylerH Jul 19 '23 at 18:37
0

I copied the body text from somewhere. And during the copy-paste process, something like space was added to the text. That led to the 400 error. I "beatify" my text and fixed the problem.

Jean Chen
  • 29
  • 1
-1
  • Sometimes it can be silly mistakes like putting a get request when it should be a post request.
  • Other times it may be incorrect syntax in the body.
  • Or as described above, typo in end points.
lexodus k
  • 97
  • 1
  • 2