Joseph Dailey Joseph Dailey - 1 year ago 116
Node.js Question

req.body empty on posts

All of a sudden this has been happening to all my projects.

Whenever I make a post in nodejs using express and body-parser

is an empty object.

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded

// parse application/json

app.listen(2000);"/", function (req, res) {
console.log(req.body) // populated!
res.send(200, req.body);

Via ajax and postman it's always empty.

However via curl

$ curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:2000/

it works as intended.

I tried manually setting
Content-type : application/json
in the former but I then always get
400 bad request

This has been driving me crazy.

I thought it was that something updated in body-parser but I downgraded and it didn't help.

Any help appreciated, thanks.

Answer Source

In Postman of the 3 options available for content type select "X-www-form-urlencoded" and it should work.

Also to get rid of error message replace:



  extended: true


The 'body-parser' middleware only handles JSON and urlencoded data, not multipart

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download