Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Firebase Cloud functions handle HTTP post method?

I have created Firebase Cloud Functions app, I created function with https.onRequest. and get data with req.body but there is not data there.

Can Firebase Cloud Functions can handle HTTP POST method?

This is my sample code:-

var functions = require('firebase-functions');  exports.testPost = functions.https.onRequest((req, res) => {     console.log(req.body); }); 

I tested by postman with POST method but didn't show result in Firebase log.

like image 940
iamcmnut Avatar asked Mar 24 '17 09:03

iamcmnut


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 Firebase cloud functions work?

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.

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.


1 Answers

Functions built on Firebase can also use Express.js routers for handling GET/POST/PUT/DELETE, etc... is fully supported by Google, and is the recommended way to implement these types of functions.

More documentation can be found here:

https://firebase.google.com/docs/functions/http-events

Here's a working example built on Node.js

const functions = require('firebase-functions'); const express = require('express'); const cors = require('cors'); const app = express();  // Automatically allow cross-origin requests app.use(cors({ origin: true }));  app.get('/hello', (req, res) => {   res.end("Received GET request!");   });  app.post('/hello', (req, res) => {   res.end("Received POST request!");   });  // Expose Express API as a single Cloud Function: exports.widgets = functions.https.onRequest(app); 

Then, run firebase deploy, and that should compile your code and create the new "widgets" function. Note: You can rename widgets to anything you want. Ultimately, it will generate a URL for calling the function.

like image 128
nukalov Avatar answered Oct 01 '22 07:10

nukalov