Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Firebase Callable Functions with HTTP?

I realised that the new Callable Cloud Functions can still be called as if they were HTTP events, i.e. they can still be reached under http://us-central1-$projectname.cloudfunctions.net/$functionname. When doing that I receive an error message in my Cloud Functions Log:

Request has invalid method. GET  

This means that HTTP-GET does not work, but is there a way to call the functions? Maybe they are using HTTP-CONNECT.

like image 836
creativecreatorormaybenot Avatar asked Mar 25 '18 13:03

creativecreatorormaybenot


People also ask

How do I call Firebase callable function?

The Cloud Functions for Firebase client SDKs let you call functions directly from a Firebase app. To call a function from your app in this way, write and deploy an HTTPS Callable function in Cloud Functions, and then add client logic to call the function from your app.

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.

What is the difference between onCall HTTP callable and onRequest HTTP request functions?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.

How do I trigger HTTP cloud function?

For Cloud Functions (1st gen): In the Trigger type field, select HTTP. In the Authentication field, select an option depending on whether you want to allow unauthenticated invocations of your function. By default, authentication is required.


2 Answers

EDIT: The details of the protocol have been formally documented now.

HTTPS Callable functions must be called using the POST method, the Content-Type must be application/json or application/json; charset=utf-8, and the body must contain a field called data for the data to be passed to the method.

Example body:

{     "data": {         "aString": "some string",         "anInt": 57,         "aFloat": 1.23     } } 

If you are calling a function by creating your own http request, you may find it more flexible to use a regular HTTPS function instead.

like image 171
bklimt Avatar answered Sep 23 '22 23:09

bklimt


You can use Firebase CLI firebase functions:shell to invoke onCall(..) functions.

A significant bonus with this approach is that from the shell you can run the functions available in your local env. - Firestore calls, etc - without having to actually deploy those functions to the cloud project.

Steps:

  1. Run cmd: firebase functions:shell
  • This will give you the prompt firebase >
  1. At the prompt call the func, and pass empty body if no params
  • firebase > someFuncAbc({})

Thanks to answer in this thread: https://stackoverflow.com/a/62051894/2162226

Yes, this answer deviates from the OP that requests how to do this with HTTP .. but adding here as an alternate way to make the calls simply from the CLI, without having to set HTTP headers, set up the HTTP client like curl or Postman, etc.

like image 25
Gene Bo Avatar answered Sep 22 '22 23:09

Gene Bo