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

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

问题描述

我需要原始请求主体能够SHA-1消化它,以验证与请求一起传递给我的Firebase Function(在Google Cloud Functions上运行)的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应用,但这似乎是作为子应用运行的,或者已经解析了请求主体的东西,就像您将普通的请求-响应回调传递给.

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天全站免登陆