question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using body-parser in express route specific got empty request body

See original GitHub issue

I tried to apply the the body parser into a specific router, and I always get empty request body.

the following is my code:

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

var jsonParser = bodyParser.json({ type: 'application/*+json'});
var router = express.Router();

router.post('/', jsonParser, function(req, res) {
  if(!req.body || req.body.length === 0) {
    console.log('request body not found');
    return res.sendStatus(400);
  }
  var user = req.body;
  console.log('request body : ' + JSON.stringify(user));
  });
});

module.exports = router;

The same request was tried with the express app with both generic and express route specific as in the example here https://github.com/expressjs/body-parser#examples and it works fine.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
dougwilsoncommented, May 12, 2015

Right, but you see, you are sending the header Content-Type: application/json, but in your code above, you are setting type: 'application/*+json'. application/json does not match application/*+json, which is why nothing is happening. You want to just remove the type option all together unless there is a specific reason you have that there.

1reaction
dougwilsoncommented, May 12, 2015

👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

req.body empty on posts - node.js - Stack Overflow
Whenever I make a post in nodejs using express and body-parser req.body is an empty object. var express = require('express') var bodyParser = ......
Read more >
Express body-parser middleware
Node.js body parsing middleware. Parse incoming request bodies in a middleware before your handlers, available under the req.body property. Note As ...
Read more >
req.body is empty in POST requests : r/node - Reddit
Hi! I know this is basically a noob question but please bear with me. I noticed that POST requests on my node server...
Read more >
express router post body empty - Industry Today
For this tutorial we're going to use the express.Router middleware as it allows us to group the route handlers for a particular part...
Read more >
express request body is undefined - You.com | The AI Search ...
You must make sure that you define all configurations BEFORE defining routes. If you do so, you can continue to use express.bodyParser() ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found