35

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.

KENdi
  • 7,576
  • 2
  • 16
  • 31
iamcmnut
  • 459
  • 1
  • 5
  • 9

4 Answers4

46

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.

nukalov
  • 1,309
  • 13
  • 18
  • The `functions.https.onCall()` is really meant for Simple 'function' type calls. E.g. addMessage(), retrieveMessage() (so NOT very Rest-like) using the Cloud Functions client. with `functions.https.onRequest()` it allows a more Rest like (post, put, delete methods) api, but you'll have to use a full fledged client. See this answer - https://stackoverflow.com/a/51477892/114549 – aaronvargas Jun 04 '19 at 15:33
  • 1
    This is clean, but it removes the overview of all the different API-calls as functions directly in firebase... – Otziii Feb 18 '21 at 20:09
14

I am planning to do the same thing. What I reckon the approach should be is to check the request.method in the function body. A probable approach can be:

if (request.method != "POST") {
     respond.status(400).send("I am not happy");
     return;
}

// handle the post request

Here's some reference to the details regarding what the request object holds: https://firebase.google.com/docs/functions/http-events

4

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;
}

Then to get data from POST request (for example JSON type) will be in the header of your request.

const postData = request.body;
// for instance
const format = req.body.format;
// query string params
let format = req.query.format;
SerzN1
  • 1,814
  • 23
  • 15
-15

Maybe your project hasn't been setup to communicate with your firebase database. Try the following from your terminal:

npm install -g firebase-tools

Then inside your project folder, run the following and login using your credentials

firebase login

Then

firebase init functions

This will create a folder with index.js, package.json and node_modules

If you are using Postman correctly the rest of your code should work.

T M
  • 516
  • 5
  • 18
  • 8
    The question is about handling post requests with Firebase cloud functions. It's unrelated with logging in to firebase using the firebase cli. – Carlos C Oct 17 '18 at 19:41
  • Question has asked about handling POST request and not how to use Firebase cli. – Shagun Verma Apr 20 '23 at 07:26