The JSON data in request body is not getting parsed using body-parser
Asked Answered
L

3

13

When I send a POST request using postman to localhost:8080/api/newUser with request body:

{name: "Harry Potter"}

At server end console.log(req.body) prints:

{ '{name: "Harry Potter"}': '' }

server.js

var express = require('express'); 
var app = express();
var router = express.Router();
var bodyParser = require('body-parser');

app.use('/', express.static(__dirname));

router.use(function(req, res, next) {
    next();
});

router
    .route('/newUser')
    .post(function(req, res) {
        console.log(req.body);
    });

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // support json encoded bodies
app.use('/api', router);

app.listen(8080);

What am I doing wrong?

Larrainelarrie answered 3/4, 2016 at 17:12 Comment(1)
what are the parameter you setting in postmanOsuna
I
11

In express.js the order in which you declare middleware is very important. bodyParser middleware must be defined early than your own middleware (api endpoints).

var express = require('express'); 
var app = express();
var router = express.Router();
var bodyParser = require('body-parser');

app.use('/', express.static(__dirname));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // support json encoded bodies

router
    .route('/newUser')
    .post(function(req, res) {
        console.log(req.body);
    });

app.use('/api', router);

app.listen(8080);
Industrialism answered 3/4, 2016 at 17:20 Comment(4)
Done @NikhilWanpalLarrainelarrie
For me the solution was setting proper header Content-Type: application/json for curlOrdinand
@michalzuber's solution worked for me as well. Both .json() and .urlencoded() look at the headers of a request and based on the Content-Type the right middleware is used. I am using jQuery's ajax and it sets the ContentType to application/x-www-form-urlencoded by default. That caused the server to not use the json middleware.Sopping
I am having this issue. Because of CORS/web fetch limitations I need to set ContentType to application/x-www-form-urlencoded, and am receiving { '{name: "Harry Potter"}': '' }. Is there a way to fix this or do I just need to JSON.parse the key?Parallelism
M
10

Change the request header

'Content-Type':'application/json'

So that bodyParser can parse the body.

*That is what works for me. i am using angular 2+ with express(body-parser)

Mudd answered 21/2, 2018 at 6:40 Comment(0)
D
0

I spent quite a bit of time trying to figure out how to pass objects from Axios as key-value pairs and eventually decided to go with an alternative because setting the Content-Type: "application/json" retuned an empty object.

If the above options don't work for you, I would consider:

  • Extracting the key (which should contain the entire object)
  • Parsing the key
  • Accessing the values of the newly created objects

This worked for me:

var obj = (Object.keys(req.body)[0])

var NewObj = JSON.parse(obj)

var name = apiWords["Key1"]

var image = apiWords["Key2"]

Dimorphous answered 7/5, 2021 at 2:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.