HTTP POST Google Cloud Functions NodeJS [英] HTTP POST Google Cloud Functions NodeJS

查看:90
本文介绍了HTTP POST Google Cloud Functions NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何编写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?

例如

  • I can send the HTTP trigger to my cloud function (https://us-central1-plugin-check-xxxx.cloudfunctions.net/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: 'https://example.com',
      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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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