HTTP POST Google 云函数 NodeJS
HTTP POST Google Cloud Functions NodeJS
如何编写 Google 云函数来接收 HTTP 请求,然后将 HTTP POST 请求发送到不同的端点?
例如,
我可以将 HTTP 触发器发送到我的云函数 (https://us-central1-plugin-check-xxxx.cloudfunctions.net/test)。我正在使用 exports.test = function helloWorld(req, res){}
来处理接收到的数据。
然后我想通过 HTTP POST 请求将处理后的数据发送到不同的端点。
到目前为止,我已经尝试使用 node-webhooks、请求和 restler 模块发送 HTTP POST,但其中 none 似乎可以正常工作。是因为这些模块与 exports.test 一起使用吗?
我的问题与 问题有关,但答案对我没有帮助。
发送到端点的数据在 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)
});
}
使用网络套接字
SOCKET.IO 是最好的选择。 https://socket.io/
但是在云函数中使用这个是不可能的。因为它分布在不同的机器上。所以跟踪是不可能的。
在用 Node.js 编写的 Cloud Functions 中,您可以使用可通过 NPM 访问的任何包。使用 Google 搜索查找此类软件包显示:
- 5 Ways to Make HTTP Requests in Node.js
- Calling a REST API from a NodeJS Script
- REST Client for Node.js
- 4 + 1 ways for making HTTP requests with Node.js: async/await edition
- Unirest for Node.js
- ...其他
您选择哪一个通常是品味问题。我个人的选择是使用能胜任且最受欢迎的那个。我将受欢迎程度等同于持续支持和更新的可能性。
正如@RenaudTarnec 在评论中提到的,问题是我的结算帐户没有配置。
Google 不允许在没有有效账单信息的情况下向外部服务器发出出站请求,以防止任何恶意 activity.
配置计费帐户后,我能够发出出站请求,并且问题中提到的所有节点模块都有效。
如何编写 Google 云函数来接收 HTTP 请求,然后将 HTTP POST 请求发送到不同的端点?
例如,
我可以将 HTTP 触发器发送到我的云函数 (https://us-central1-plugin-check-xxxx.cloudfunctions.net/test)。我正在使用
exports.test = function helloWorld(req, res){}
来处理接收到的数据。然后我想通过 HTTP POST 请求将处理后的数据发送到不同的端点。
到目前为止,我已经尝试使用 node-webhooks、请求和 restler 模块发送 HTTP POST,但其中 none 似乎可以正常工作。是因为这些模块与 exports.test 一起使用吗?
我的问题与
发送到端点的数据在 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)
});
}
使用网络套接字 SOCKET.IO 是最好的选择。 https://socket.io/
但是在云函数中使用这个是不可能的。因为它分布在不同的机器上。所以跟踪是不可能的。
在用 Node.js 编写的 Cloud Functions 中,您可以使用可通过 NPM 访问的任何包。使用 Google 搜索查找此类软件包显示:
- 5 Ways to Make HTTP Requests in Node.js
- Calling a REST API from a NodeJS Script
- REST Client for Node.js
- 4 + 1 ways for making HTTP requests with Node.js: async/await edition
- Unirest for Node.js
- ...其他
您选择哪一个通常是品味问题。我个人的选择是使用能胜任且最受欢迎的那个。我将受欢迎程度等同于持续支持和更新的可能性。
正如@RenaudTarnec 在评论中提到的,问题是我的结算帐户没有配置。
Google 不允许在没有有效账单信息的情况下向外部服务器发出出站请求,以防止任何恶意 activity.
配置计费帐户后,我能够发出出站请求,并且问题中提到的所有节点模块都有效。