Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use firebase cloud function to send POST request to non-google server

I was wondering if its possible to use a firebase cloud function to send a post request to a non-google server (from what I can find I need to be on the blaze plan in order to interact with non google servers)

Basically I want to POST to an external server running on an arduino whenever a value is added to my database.

I have looked through the docs and found examples of having a cloud function respond to an HTTP post request (HTTP cloud functions) but can't seem to find any examples of posting to an external server. Is this possible?

like image 549
Stone Preston Avatar asked Apr 14 '17 16:04

Stone Preston


People also ask

Does Firebase work on HTTP?

You can connect an HTTP function to Firebase Hosting. Requests on your Firebase Hosting site can be proxied to specific HTTP functions. This also allows you to use your own custom domain with an HTTP function. Learn more about connecting Cloud Functions to Firebase Hosting.

Can I use Cloud Functions Firebase for free?

Cloud Functions includes a perpetual free tier for invocations to allow you to experiment with the platform at no charge. Note that even for free tier usage, we require a valid billing account.

When should I use Firebase cloud function?

You should use Cloud Functions for Firebase if you're a developer building a mobile app or mobile web app. Firebase gives mobile developers access to a complete range of fully managed mobile-centric services including analytics, authentication and Realtime Database.


4 Answers

Note: request package has been deprecated as stated in the npm page request-npm. Consider using other alternatives like axios

This can be done using the request module:

// import the module var request = require('request');  // make the request request('put your external url here', function (error, response, body) {     if (!error && response.statusCode == 200) {         //here put what you want to do with the request     } }) 

NOTE: This will only work on paid plans. It is not possible to call non-google APIs using the free Spark plan as explained on the Firebase pricing page:

The Spark plan allows outbound network requests only to Google-owned services. Inbound invocation requests are allowed within the quota. On the Blaze plan, Cloud Functions provides a perpetual free tier. The first 2,000,000 invocations, 400,000 GB-sec, 200,000 CPU-sec, and 5 GB of Internet egress traffic is provided for free each month. You are only charged on usage past this free allotment. Pricing is based on total number of invocations, and compute time. Compute time is variable based on the amount of memory and CPU provisioned for a function. Usage limits are also enforced through daily and 100s quotas. For more information, see Cloud Functions Pricing.

like image 187
stodi Avatar answered Sep 30 '22 22:09

stodi


You need to install the package. Go to Firebase-Funcions directory in Terminal and type

npm install request 

OR

npm install request-promise 

Use this example for test: https://www.npmjs.com/package/request

like image 40
RodolfoNeto Avatar answered Sep 30 '22 21:09

RodolfoNeto


Remember to install the module within the functions folder!

cd functions
npm i --save request
like image 22
Cesare Avatar answered Sep 30 '22 21:09

Cesare


For those of you who want to post with a JSON body this is how you can do it. (I know I needed this a while ago)

export function postWithBodyToExternalUrl(url: string, bdy: any): Promise<ReqResponse> {

  const request = require('request');

  const options = {
    url: url,
    json: true
  };
  return new Promise(function (resolve, reject) {
    request(options, function (err, resp) {
      if (err) {
        console.log(err);
        reject({ err: err });
      }
      resolve(bdy);
    });
  });
}
like image 34
Ruan Avatar answered Sep 30 '22 22:09

Ruan