arpit joshi
arpit joshi

Reputation: 2134

Request body is null for POST request in NodeJS

I have the below code. when I make a Post request via Postman I get req.body as undefined.

Post Request is http://localhost:1702/es.

Body:

{
  "ip_a":"191.X.X.XX",
  "pkts":34    
}

and Content-Type:"application/json". I also used application/x-www-form-urlencoded but got the same result.

My app.js is:

var express = require('express');
var es=require('./routes/es');
var app = express();
app.post('/es',es.postesdata);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

And my file where I am receiving a request body null is:

exports.postesdata=function(req,res){

    var body=req.body;

    console.log(body);//Getting Undefined here 

}

Am I doing something wrong here?

Upvotes: 3

Views: 10047

Answers (1)

jmunsch
jmunsch

Reputation: 24089

express runs middleware in order try:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/es',es.postesdata);

Upvotes: 9

Related Questions