如何从 Google Cloud Functions (nodeJS) 发送 HTTP 请求 [英] How to send a HTTP request from Google Cloud Functions (nodeJS)

查看:18
本文介绍了如何从 Google Cloud Functions (nodeJS) 发送 HTTP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,但我是云功能/节点编程的新手,还没有找到合适的文档.

This is probably a simple question but I'm new to cloud functions/Node programming and haven't found the right documentation yet.

如何编写一个接收 HTTP 请求但随后将 HTTP 请求发送到不同端点的 Google 云函数?例如,我可以将 HTTP 触发器发送到我的云函数 (https://us-central1-plugin-check-xxxx.cloudfunctions.net/HelloWorldTest).在项目的后期,我将弄清楚如何实现延迟.但后来我想用一个新的 HTTP 请求响应另一个端点 (https://maker.ifttt.com/trigger/arrive/with/key/xxxx).我怎么做?

How do I write a Google cloud function that will receive a HTTP request but then send a HTTP request to a different endpoint? For example, I can send the HTTP trigger to my cloud function (https://us-central1-plugin-check-xxxx.cloudfunctions.net/HelloWorldTest). Later in the project I'll figure out how to implement a delay. But then I want to respond with a new HTTP request to a different endpoint (https://maker.ifttt.com/trigger/arrive/with/key/xxxx). How do I do that?

exports.helloWorld = function helloWorld(req, res) {
  // Example input: {"message": "Hello!"}
  if (req.body.message === undefined) {
    // This is an error case, as "message" is required.
    res.status(400).send('No message defined!');
  } else {
    // Everything is okay.
    console.log(req.body.message);
    res.status(200).send('Success: ' + req.body.message);
    // ??? send a HTTP request to IFTTT endpoint here
  }
};

推荐答案

这是我在 Chetan Kanjani 的帮助下设法获得的代码.当我向我的 Google Cloud 函数端点发送一条短信时,它会回复一条短信给 IFTTT(另一个端点).

Here is the code that I managed to get working with help from Chetan Kanjani. When I send a text message to my Google Cloud function endpoint, it replys with a text message to IFTTT (a different endpoint).

const request = require('request');

exports.helloWorld = function helloWorld(req, res) {
  // Example input: {"message": "Hello!"}
  if (req.body.message === undefined) {
    // This is an error case, as "message" is required.
    res.status(400).send('No message defined!');
  } else {
    // Everything is okay.
    console.log(req.body.message);

    request.get('https://maker.ifttt.com/trigger/arrival/with/key/xxxx', function (error, response, body) {
      console.log('error:', error); // Print the error if one occurred 
      console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
      console.log('body:', body); //Prints the response of the request. 
    });
    res.status(200).send("Success");
  }
};

我还必须更改 package.json 文件以包含请求包.它已经有了 sample-http 包,我添加了依赖项:

I also had to change the package.json file to include the request package. It already had the sample-http package, I added the dependencies:

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "request": "^2.81.0"
  }
}

我仍然不确定 console.log 函数在哪里打印信息.这可能对将来的调试有所帮助.

I'm still not sure where the console.log function prints out the information. That might be helpful for future debugging.

这篇关于如何从 Google Cloud Functions (nodeJS) 发送 HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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