Lloyd
Lloyd

Reputation: 1403

Make HTTPS call within Firebase Function

I am trying to make a call within a firebase function to a locally managed server. I am not super familiar with node as a development environment so I am not sure what is the issue.

const functions = require('firebase-functions');
const https = require('http');
exports.testPost = functions.https.onRequest((req, res) => {
  var options = {
    host: 'localdevserver.edu',
    port: 80,
    path: '/my/endpoint'
  };
  let data = '';

  http.get(options, function(resp){
    resp.on('data', function(chunk){
      //do something with chunk
      data += chunk;
      resp.on('end', console.log("dones"));
    });
  }).on("error", function(e){
    console.log("Got error: " + e.message);
  });
});

When I look in the Firebase Functions Log, it says either timeout or no reject defined.

Upvotes: 0

Views: 1343

Answers (2)

Mike Brian Olivera
Mike Brian Olivera

Reputation: 1601

You can use SYNC-REQUEST

npm install sync-request

var request = require('sync-request');
var res = request('GET', 'http://google.com');
console.log(res.body.toString('utf-8'));

the function would be something like this:

exports.testPost = functions.https.onRequest((req, res) => {

    var request = require('sync-request');
    var res = request('GET', 'http://google.com');
    var res = res.body.toString('utf-8');

   resp.on(res, console.log("dones"));
});

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317467

With HTTP type functions, you need to send a response to the client in order to terminate the function. Otherwise it will time out.

res.send("OK");

Please read the documentation for more details.

Upvotes: 3

Related Questions