Famic Tech
Famic Tech

Reputation: 280

Google Cloud functions call URL hosted on Google App Engine

I have a firebase database that I wish to create a cloud function that triggers when adding a child node to the parent node , which should call a url with the parameters of the child node added in the parent node.

The URL which would be called is a NodeJS Express app hosted in Google App Engine.

How do I do that, if it is even possible?

Upvotes: 3

Views: 1165

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

You can use the node.js request library to do so.

Since, inside your Cloud Function, you must return a Promise when performing asynchronous tasks, you will need to use an interface wrapper for request, like request-promise.

You could do something along these lines:

.....
var rp = require('request-promise');
.....

exports.yourCloudFucntion = functions.database.ref('/parent/{childId}')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const createdData = snapshot.val();

      var options = {
          url: 'https://.......',
          method: 'POST',
          body: ....
          json: true // Automatically stringifies the body to JSON
      };

      return rp(options);

    });

If you want to pass parameters to the HTTP(S) service/endpoint you are calling, you can do it through the body of the request, like:

      .....
      const createdData = snapshot.val();

      var options = {
          url: 'https://.......',
          method: 'POST',
          body: {
              some: createdData.someFieldName
          },
          json: true // Automatically stringifies the body to JSON
      };
      .....

or through some query string key-value pairs, like:

      .....
      const createdData = snapshot.val();
      const queryStringObject = { 
         some: createdData.someFieldName,
         another: createdData.anotherFieldName
      };

      var options = {
          url: 'https://.......',
          method: 'POST',
          qs: queryStringObject
      };
      .....

Upvotes: 2

Related Questions