If you are new to Firebase functions, please read the functions get started document. I have a chance to play around with HTTP request and Firebase Functions for a while, and there are few tips that I would like to share.

How to start a HTTP Request function

You started by declare it on the index.js file

exports.request = functions.https.onRequest((request, response) => {

// ...

});

The function is trigger by calling it directly from the browser, methods or using a HTTP service. There is a very good article about how to schedule HTTP request to your firebase functions that worths reading.

2. How to check for request methods

Firebase functions support GET, POST, PUT, DELETE, and OPTIONS method, and you can check what kind of methods that trigger your function.

// Check for POST request

if(request.method !== "POST"){

res.status(400).send('Please send a POST request');

return;

}

3. How to get data from POST request

The data (for example JSON type) will be in the header of your request.

let data = request.body;

4. How to send a POST request from your app / web

This is an example from my Angular app that send POST request to trigger the firebase functions

// Using Angular HttpClient to send POST request const httpOptions = {

headers: new HttpHeaders({

'Content-Type': 'application/json',

'Authorization': 'secret-key'

})

}; let data = {name: 'Dale Nguyen'}; this.http.post('https://us-central1-dale-nguyen.cloudfunctions.net/request', data, httpOptions)

.subscribe(

res => {

console.log(res);

},

err => {

console.log("Error occured");

}

)

5. A way to add extra security to your functions

Thanks for the post about HTTP request schedule above, the is a few method that you can add extra security to your app.

You need to create a unique key for security

npm install -g crypto

node -e "console.log(require('crypto').randomBytes(20).toString('hex'))"

Then add the key to your firebase config

functions:config:set app_name.key='secret-key'

The way you can use the key to verify the app can be similar with the article above, you can check it directly by comparing it on firebase server.

// POST headers before sending to firebase server const httpOptions = {

headers: new HttpHeaders({

'Content-Type': 'application/json',

'Authorization': 'secret-key'

})

}; // firebase functions check let key = functions.config().app_name.key; let request_key = request.get('authorization'); if(key === request_key){

console.log('Awesome!!!')

} else {

response.status(400).send('You shall not pass!!!');

return;

}

6. Dealing with CORS — ‘Access-Control-Allow-Origin’

Thanks StackOverFlow, this can be handled easily.

Before importing CORS, we need to install it first.

npm install cors // Thanks John Theo for remind this step :)

Then put it in the index.js file

// Add CORS to your index.js

const cors = require('cors')({origin: true}); // Put this line to your function

// Automatically allow cross-origin requests

cors(req, res, () => {});

7. Send GET request with parameters (Angular)

/*

* Send GET request with parameters from Angular

* https://angular.io/api/http/RequestOptionsArgs

*/



import { Http, Headers } from '@angular/http';



// Prepare the header

let headers: Headers = new Headers();

headers.set('parameter-name' , 'parameter-value);



// Send request with parameters

this.http.get('url', {

headers: headers

}).subscribe(res => resolve(res.json()));

In order to get the parameters from the header, you can use this code inside the firebase functions

// In order get the request value

let params = req.headers['parameter-name'];

That’s all for now. I will update when I encounter another interesting issue ;)

Please check my github for more interesting snippets about firebase functions.

https://github.com/dalenguyen/firebase-functions-snippets