TSR
TSR

Reputation: 20386

req.body is empty express js

I have been spending hours trying to figure out why req.body is empty. I have looked everywhere on stackoverflow and tried everything but no luck.

I tried setting:

app.use(bodyParser.urlencoded({extended: false})); //false

but it did not change anything

Here is app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var ajax = require('./routes/ajax');


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.disable('etag'); //disable cache control

app.use('/', index);
app.use('/ajax', ajax);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;

Now let's have a look at ajax.js

var express = require('express');
var router = express.Router();
router.post('/1/kyc/form', function (req, res, next) {
    console.log(req.body) //prints {}
});

This is the request done by the client:

enter image description here

Upvotes: 3

Views: 11097

Answers (1)

robertklep
robertklep

Reputation: 203231

The Content-Type header of your request is invalid:

Content-Type: application/json;

The trailing semicolon shouldn't be there. So it should be this:

Content-Type: application/json

FWIW, it's not bodyParser.urlencoded that's being used here; because the body content is JSON, it's bodyParser.json that handles processing the request body. But it's perfectly okay to have both of these body parsers active.

EDIT: if what the client sends is beyond your control (or it's too much of a hassle to fix it client-side), you can add an additional middleware to Express that will fix the invalid header:

app.use(function(req, res, next) {
  if (req.headers['content-type'] === 'application/json;') {
    req.headers['content-type'] = 'application/json';
  }
  next();
});

Make sure that you do this before the line that loads bodyParser.json.

Upvotes: 8

Related Questions