Skip to content Skip to sidebar Skip to footer

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. Express.js POST req.body empty E

Solution 1:

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.


Post a Comment for "Req.body Is Empty Express Js"