Search code examples
node.jsjsonexpressbody-parser

send json with request to express in node


I have 2 node js apps one sending a post request as follows:

request.post({
    url: url,
    headers: {"content-type": "application/json"},
    json: {a:1,b:2}
},
    function (error, response, body) {
        //..
    }
);

and the other is trying to handle it with express and body-parser:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/verify', (req, res,cb = (err, res) => {}) => {
    var data =  req.body; //returns empty json!
    // ..
}

the problem is that at the receiving end I can't retrieve the json data I'm looking for. Does any body know what I'm missing?


Solution

  • Adding this to your server side code should work:

    app.use(bodyParser.json())