Vah*_*afi 4 node.js express body-parser
我的 index.js 中有以下内容:
app.use(bodyParser.json());
Run Code Online (Sandbox Code Playgroud)
但 Stripe webhooks 想要这样:
将原始正文与内容类型 application/json 相匹配
如果我将 index.js 更改为以下内容:
app.use(bodyParser.raw({type: 'application/json'}));
Run Code Online (Sandbox Code Playgroud)
效果很好。但我所有其他 API 路线将不再起作用。这是我的路线:
router.route('/stripe-events')
.post(odoraCtrl.stripeEvents)
Run Code Online (Sandbox Code Playgroud)
如何更改为仅此 api 路由的原始主体?
小智 11
您可以通过执行以下操作同时访问两者:
app.use(bodyParser.json({
verify: (req, res, buf) => {
req.rawBody = buf
}
}))
Run Code Online (Sandbox Code Playgroud)
现在,原始正文可在req.rawBody上使用,并且 JSON 解析数据可在req.body上使用。
| 归档时间: |
|
| 查看次数: |
2088 次 |
| 最近记录: |