Google Cloud functions call URL hosted on Google App Engine

前端 未结 1 667
天命终不由人
天命终不由人 2020-12-19 13:48

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

相关标签:
1条回答
  • 2020-12-19 14:15

    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
          };
          .....
    
    0 讨论(0)
提交回复
热议问题