3

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

TSR
  • 17,242
  • 27
  • 93
  • 197

1 Answers1

8

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.

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • How do I tell express js to consider application/json; the same as application/json ( I know it is much easier to fix the client request but I just wonder if this problem can be fixed from the back end side? ) – TSR Sep 03 '18 at 16:34
  • 1
    Thanks that worked for me! I inspected the request via fiddler and I extracted the mime type. Then I've put that mime type to the conditional. `if (req.headers["content-type"] === "text/plain;charset=UTF-8") {` Now I can finally parse the request body – gdyrrahitis Aug 16 '19 at 19:50