简体   繁体   中英

Node.js unable to read POST JSON data in my webhook

I have a node.js + Express application. It has a webhook that I have provided to a third party service. The service sends a POST request to my webhook with JSON body which looks something like this:

{"split_info" : "null", "customerName" : "Merchant Name", "additionalCharges" : "null", "paymentMode":"CC", "hash":"a31ff1b91fd9b8ae9c82f38b02348d21fsdfd86cc828ac9a0acf82050996372cc656de3db0fe3bf9af52b73a182a77787241f3e19ec893391607301b03e70db8", "status" : "Release Payment", "paymentId" : "551731" , "productInfo":"productInfo", "customerEmail":"test@gmail.com", "customerPhone":"9876543212", "merchantTransactionId":"jnn", "amount":"100.0", "udf2":"null", "notificationId" :"4", "udf1":"null", "udf5":"null", "udf4":"null", "udf3":"null","error_Message":"No Error"}

I am using body-parser module to read POST data. However when I do req.body it gives [object Object], if I do JSON.stringify(req.body), it gives {} ie empty. If I try to access the keys in the response like req.body.paymentMode then it gives undefined.

Here is my router code for the webhook: mywebhook.js

var express = require('express');
var router = express.Router();

router.post('/success', function(req, res){

    //this is where I need to strip the JSON request
    //req.body or JSON.stringify(req.body) or anything that works
    //if everything is okay then I send
    res.sendStatus(200);

});

module.exports = router;

My app.js looks like this:

var express = require('express');                               
var exphbs = require('express-handlebars');
var router = express.Router();                                  
var bodyParser = require('body-parser');

var mywebhook = require('./routes/mywebhook');  

var app = express(); 

.
.
.
app.use(bodyParser.urlencoded({'extended':'true'}));            // parse application/x-www-form-urlencoded
app.use(bodyParser.json());                                     // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json


app.use('/callwebhook', mywebhook);

.
.
.
so on           

Pretty sure I am missing something or doing something wrong, but I am not able to figure it out.

Thanks.

I finally found what was going on.

The way the body-parser works is that it will only try to parse a request in which they understand the Content-Type. This is mainly so you can stack them (app.use multiple parser types without conflict) and also because you typically don't want to parse a request that's going to fail (Content-Type: text/html is unlikely to ever get through a JSON.parse, for example).

I ended up getting sent */*; charset=UTF-8 */*; charset=UTF-8 which is not even a valid Content-Type header value period. The body-parser module refused to accept it, since that is gibberish. This module does allow you to setup a function that lets you put any custom logic you want to perform the filtering.

I had to put the body parser in my router code just for this webhook case.

var bodyParser = require('body-parser');
var customParser = bodyParser.json({type: function(req) {
    return req.headers['content-type'] === '*/*; charset=UTF-8';
}});

router.post('/success', customParser, function(req, res){
    console.log(JSON.stringify(req.body));
});

@svens thank you for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM