如何在 Google Cloud Function 中获取原始请求正文? [英] How can I get the raw request body in a Google Cloud Function?

查看:27
本文介绍了如何在 Google Cloud Function 中获取原始请求正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要原始请求正文能够对其进行 SHA-1 消化,以验证与请求一起传递给我的 Firebase 函数(在 Google Cloud 函数上运行)的 Facebook webhook X-Hub-Signature 标头.

I need the raw request body to be able to SHA-1 digest it to validate the Facebook webhook X-Hub-Signature header that's passed along with the request to my Firebase Function (running on Google Cloud Functions).

问题在于,在这种情况下(带有 Content-Type: application/json 标头)GCF 会使用 bodyParser.json() 自动解析正文,这会消耗来自流的数据(意味着它不能在 Express 中间件链中再次使用)并且只提供解析的 javascript 对象作为 req.body.原始请求缓冲区被丢弃.

The problem is that in cases like this (with a Content-Type: application/json header) GCF automatically parses the body using bodyParser.json() which consumes the data from the stream (meaning it cannot be consumed again down the Express middleware chain) and only provides the parsed javascript object as req.body. The raw request buffer is discarded.

我试图为 functions.https.onRequest() 提供一个 Express 应用程序,但这似乎是作为子应用程序运行的,或者请求正文已经被解析的东西,就像当你将一个普通的请求-响应回调传递给 onRequest().

I have tried to provide an Express app to functions.https.onRequest(), but that seems to be run as a child app or something with the request body already being parsed, just like when you pass a plain request-response callback to onRequest().

有什么方法可以禁止 GCF 为我解析正文?或者我可以以某种方式指定我自己的 verify 回调到 bodyParser.json()?还是有其他方法?

Is there any way to disable GCF from parsing the body for me? Or could I somehow specify my own verify callback to bodyParser.json()? Or is there some other way?

PS:一周前我第一次联系了 Firebase 支持,但由于那里没有回复,我现在在这里尝试.

PS: I first contacted Firebase support about this a week ago, but for lack of a response there I'm trying it here now.

推荐答案

现在你可以从 req.rawBody 获取原始正文.它返回 Buffer.有关详细信息,请参阅文档.

Now you can get the raw body from req.rawBody. It returns Buffer. See documentation for more details.

感谢 Nobuhito Kurose 在 评论.

Thanks to Nobuhito Kurose for posting this in comments.

这篇关于如何在 Google Cloud Function 中获取原始请求正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆