我该如何编写Google Cloud Function来接收HTTP请求,然后将HTTP POST请求发送到其他端点?
How do I write a Google Cloud Function that will receive a HTTP request and then send a HTTP POST request to a different endpoint?
例如
-
我可以将HTTP触发器发送到我的云函数( https ://us-central1-plugin-check-xxxx.cloudfunctions/test ).我正在使用exports.test = function helloWorld(req, res){}处理接收到的数据.
I can send the HTTP trigger to my cloud function (us-central1-plugin-check-xxxx.cloudfunctions/test). I am using exports.test = function helloWorld(req, res){} to process the data received.
然后我想将具有HTTP POST请求的已处理数据发送到另一个端点.
And then I want to send the processed data with a HTTP POST request to a different endpoint.
到目前为止,我已经尝试通过 node-webhooks发送HTTP POST,请求& restler 模块,但它们似乎都不起作用.是因为这些模块与 exports.test 一起使用?
By far I have tried sending HTTP POST with node-webhooks, request & restler modules but none of them seem to work. Is it because these modules are used in conjunction with exports.test ?
我的问题与此问题,但答案并没有帮助我.
My question is related to this question but the answers didn't help me.
要发送到终结点的数据位于 json &中. 内容类型:application/json .
The data being sent to endpoint is in json & Content-type: application/json.
var request = require('request'); //also tried for node-webhook, restler modules exports.test = function(req, res) { //processing of received json data from source A. } function sendToEndpoint(processed_data) { let abc = processed_data; //send processed data to source B request.post({ uri: 'example', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(abc) }); }推荐答案
正如@RenaudTarnec在评论中提到的那样,问题在于未配置我的计费帐户.
As @RenaudTarnec mentioned in the comments the problem was that my billing account was not configured.
Google不允许在没有有效的帐单信息的情况下向外部服务器发出出站请求,以防止任何恶意活动.
Google doesn't allow outbound requests to external servers without valid billing info so as to prevent any malicious activity.
配置计费帐户后,我可以发出出站请求,问题中提到的所有节点模块都可以工作.
After configuring a billing account I was able to make outbound requests and all the node-modules mentioned in the question worked.
更多推荐
HTTP POST Google Cloud Functions NodeJS
发布评论