How to send an HTTP request from Google Virtual Functions (nodeJS)

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

How do I write a google cloud function that will receive an HTTP request but then send the HTTP request to a different endpoint? For example, I can send an HTTP trigger to my cloud function ( https://us-central1-plugin-check-xxxx.cloudfunctions.net/HelloWorldTest ). Later in the project, I will figure out how to implement the 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 should I do it?

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
  }
};

      

+3


source to share


3 answers


Here is the code I was able to work with using Chetan Kanjani. When I send a text message to the Google Cloud feature endpoint, it responds with a text message to IFTTT (another 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");
  }
};

      

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



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

      

I'm still not sure where the console.log function is outputting information. This may be useful for future debugging.

+7


source


The request module uses callbacks. If you want to use JavaScript promises instead, the Axios module provides equivalent functionality.



+3


source


Use https://www.npmjs.com/package/request .

var request = require('request');
request.get('https://maker.ifttt.com/trigger/arrive/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. 
});

      

+2


source







All Articles