Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud function http request to another firebase cloud function

I have a main firebase cloud function with main functionalities. But I need to create another one more specific that uses the functionalities on the main cloud function. Can I request a http Post or Get from one firebase cloud function to another firebase cloud function with a free account?

I tried to make it but I get the 'ENOTFOUND' message, I want to know if is something wrong with my code or it is just a limitation of the free account.

index.js
'use strict';
const functions = require('firebase-functions');

const express = require('express');
const app = express();

app.post('/test', function(req, res) {
    var request = require('request')

    var options = {
      method: 'post',
      body: req.body, // re-send the incoming body to the main cloud function
      json: true, 
      url: '<main cloud url>',
      headers: req.headers // re-send the incoming headers the main cloud function
    }
    request(options, function (err, response, body) {
      if (err || response.statusCode != 200) {
        res.status(400).send(err); //re-send the received error to the client
        return
      }
      res.send(body); //re-send the received response to the client
    });
});

exports.checkAtivation = functions.https.onRequest(app);
like image 984
Lucas Câmara Avatar asked Jul 19 '17 12:07

Lucas Câmara


1 Answers

It's currently not possible on the Spark plan to invoke an HTTP Cloud Function from another HTTP Cloud Function. You will have to upgrade to the Blaze plan.

It mostly doesn't make sense for one project's functions to invoke other functions over HTTP. All the code in the project can be shared between all the functions if you simply abstract the logic away into a single function or module that all the endpoints can share.

like image 77
Doug Stevenson Avatar answered Oct 26 '22 23:10

Doug Stevenson