Search code examples
node.jsjsonexpresspostbody-parser

req.body empty in express 4x using app.use(express.json()) in vscode REST client


I was working on my express backend and was able to console.log and send back body data on post request on res.send(req.body) but after few moments with some changes the code doesn't seemed to work at all and retuned empty string on sending post request like this:

const express = require('express');

const app = express();
app.use(express.json())
app.post('/profile', function(req, res) {
    console.log(req.body);
    res.send(req.body);
});
app.listen(5000);

Result

Request made with VScode Rest client Extension

enter image description here

And even instead of using app.use(express.json()) I also tried body-parser which also returns the same result.

So, what should I do and is this problem with only express 4x


Solution

  • I just had the same issue, It's a minor thing but hard to figure out.

    It's all about the spaces, you shouldn't have spaces in the wrong places.

    Not working : enter image description here

    Working :

    enter image description here