Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an HTTP request in Cloud Functions for Firebase?

I am trying to make a call to apples receipt verification server using Cloud Functions for Firebase. Any idea how to make an HTTP call?

like image 924
Rashid Khan Avatar asked Apr 19 '17 04:04

Rashid Khan


People also ask

Does Firebase use HTTP requests?

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.

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.

Can Firebase run code?

Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment.

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.


1 Answers

Keep in mind that your dependency footprint will affect deployment and cold-start times. Here's how I use https.get() and functions.config() to ping other functions-backed endpoints. You can use the same approach when calling 3rd party services as well.

const functions = require('firebase-functions'); const https = require('https'); const info = functions.config().info;  exports.cronHandler = functions.pubsub.topic('minutely-tick').onPublish((event) => {     return new Promise((resolve, reject) => {         const hostname = info.hostname;         const pathname = info.pathname;         let data = '';         const request = https.get(`https://${hostname}${pathname}`, (res) => {             res.on('data', (d) => {                 data += d;             });             res.on('end', resolve);         });         request.on('error', reject);     }); }); 
like image 64
Dustin Avatar answered Oct 07 '22 03:10

Dustin