Node.js无法读取我的webhook中的POST JSON数据(Node.js unable to read POST JSON data in my webhook)

编程入门 行业动态 更新时间:2024-10-24 22:20:01
Node.js无法读取我的webhook中的POST JSON数据(Node.js unable to read POST JSON data in my webhook)

我有一个node.js + Express应用程序。 它有一个我已经提供给第三方服务的webhook。 该服务发送POST请求到我的webhook与JSON正文看起来像这样:

{“split_info”:“null”,“customerName”:“Merchant Name”,“additionalCharges”:“null”,“paymentMode”:“CC”,“hash”:“a31ff1b91fd9b8ae9c82f38b02348d21fsdfd86cc828ac9a0acf82050996372cc656de3db0fe3bf9af52b73a182a77787241f3e19ec893391607301b03e70db8”,“status” ,“paymentId”:“551731”,“productInfo”:“productInfo”,“customerEmail”:“test@gmail.com”,“customerPhone”:“9876543212”,“merchantTransactionId”:“jnn”,“金额”:“ “udf2”:“null”,“notificationId”:“4”,“udf1”:“null”,“udf5”:“null”,“udf4”:“null”,“udf3” ,“error_Message”:“无错误”}

我正在使用body-parser模块来读取POST数据。 然而,当我做req.body它给[object Object],如果我做JSON.stringify(req.body),它会给{}例如空。 如果我尝试访问req.body.paymentMode这样的响应中的键,那么它会给出未定义的。

这是我的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

很确定我错过了什么或做错了什么,但我无法弄清楚。

谢谢。

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 {} i.e. 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.

最满意答案

我终于找到了发生的事情。

body-parser的工作方式是它只会尝试解析他们理解Content-Type的请求。 这主要是因为你可以堆叠它们(app.use多个解析器类型而没有冲突),也是因为你通常不想解析会失败的请求(Content-Type:text / html不太可能通过例如JSON.parse)。

我结束了发送*/*; charset=UTF-8 */*; charset=UTF-8 ,它甚至不是有效的Content-Type标题值周期。 body-parser模块拒绝接受它,因为这是无用的。 这个模块允许你设置一个函数,让你可以放置任何你想要执行过滤的自定义逻辑。

我必须将身体解析器放入我的路由器代码中才能用于此webhook案例。

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感谢你的帮助。

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 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.

更多推荐

本文发布于:2023-08-07 11:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   POST   webhook   Node   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!