Node.js无法读取我的Webhook中的POST JSON数据 [英] Node.js unable to read POST JSON data in my webhook
问题描述
我有一个node.js + Express应用程序.它已提供给第三方服务一个webhook.该服务将带有JSON正文的POST请求发送到我的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":商人名称", "additionalCharges":"null","paymentMode":"CC", "hash":"a31ff1b91fd9b8ae9c82f38b02348d21fsdfd86cc828ac9a0acf82050996372cc656de3db0fe3bf9af52b73a182a77787241f3e19ec893391607301b03e70db8" "status":释放付款","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":否 错误"}
{"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"}
我正在使用body-parser模块读取POST数据.但是,当我执行req.body时,它将给出[object Object],如果我执行JSON.stringify(req.body),则将给出{},即为空.如果我尝试访问响应中的键(例如req.body.paymentMode),则会显示未定义的内容.
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 {} i.e. empty. If I try to access the keys in the response like req.body.paymentMode then it gives undefined.
这是我的Webhook路由器代码: mywebhook.js
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;
我的 app.js 看起来像这样:
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.
谢谢.
推荐答案
我终于找到了正在发生的事情.
I finally found what was going on.
主体解析器的工作方式是,它将仅尝试解析他们理解Content-Type的请求.这主要是因为您可以堆叠它们(应用程序使用多种解析器类型而不会发生冲突),并且还因为您通常不希望解析将失败的请求(Content-Type:text/html不太可能通过例如JSON.parse.)
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).
我最终被发送了*/*; charset=UTF-8
,这甚至不是一个有效的Content-Type标头值周期.主体分析器模块拒绝接受它,因为那是乱码.该模块确实允许您设置一个函数,该函数允许您放置要执行过滤的任何自定义逻辑.
I ended up getting sent */*; 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.
仅在此Webhook情况下,我必须将正文解析器放入路由器代码中.
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谢谢您的帮助.
@svens thank you for your help.
这篇关于Node.js无法读取我的Webhook中的POST JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!