我有一个node.js + Express应用程序。它有一个我已经提供给第三方服务的webhook。该服务发送POST请求我的网络挂接与JSON的身体看起来是这样的:Node.js无法读取我的webhook中的POST JSON数据
{“split_info”:“空”,“客户名称”:“商户名称”, “additionalCharges”:“空” “paymentMode”: “CC”, “哈希”: “a31ff1b91fd9b8ae9c82f38b02348d21fsdfd86cc828ac9a0acf82050996372cc656de3db0fe3bf9af52b73a182a77787241f3e19ec893391607301b03e70db8”, “状态”: “释放付款”, “paymentId”: “551731”, “productInfo”: “productInfo”, “customerEmail”:” test123“, ”customerPhone“:”9876543212“,”merchantTransactionId“:”jnn“, ”amount“:”100.0“,”udf2“:”null“,”notificationId“:”4“ udf1“:”null“, ”udf5 “:” 空”, “udf4”: “空”, “udf3”: “空”, “ERROR_MESSAGE”: “没有 错误”}
我使用身体的解析器模块读取POST数据。然而,当我做req.body它给[object Object],如果我做JSON.stringify(req.body),它给出{},即空。如果我尝试访问req.body.paymentMode这样的响应中的键,那么它会给出未定义的。
这里是我的网络挂接路由器代码: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
很肯定我失去了一些东西,或做有什么不对,但我无法弄清楚。
谢谢。
你确定请求的Content-type是'application/vnd.api + json'吗?这对我来说似乎很奇怪。 – svens
你是对的,当我使用hookbin.com测试第三方webhook时,它说内容类型是:*/*。所以我更改我的app.js中的内容类型为app.use(bodyParser.json({type:'*/*'})); – codeinprogress
好吧,我将类型更改为'*/*',但req.body仍为空。 – codeinprogress