0
votes

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?

1
Please tell what is url ?Mahesh Bhatnagar
localhost, the other node js app that is running the expressmid
Please share full urlMahesh Bhatnagar
Please use this code let options = { url: 'http://mockbin.com/request', form: { email: '[email protected]', password: 'myPassword' } }; request.post(options, callback);Mahesh Bhatnagar

1 Answers

0
votes

Adding this to your server side code should work:

app.use(bodyParser.json())