Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase cloud function post request body

I am making an http request from an angular form like that:

  this.http.post(this.endPoint, formData, { responseType: 'text' }).subscribe(
    res => {
      console.log(res);
    }
  )

And I have a simple cloud function:

const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });

exports.test = functions.https.onRequest((req, res) => {

    cors(req, res, () => {
        const data = req.body;
        res.send(`Hello from Firebase! ${data}`);
    });

})

However the req.body does not work and i get this response :

Hello from Firebase! [object Object]

Is there any idea why this happens?

like image 266
Giannis Savvidis Avatar asked Jan 19 '19 14:01

Giannis Savvidis


1 Answers

If you're trying to print the req.body value, you first have to convert it from a JavaScript object to a string:

res.send(`Hello from Firebase! ${JSON.stringify(data)}`);
like image 97
Frank van Puffelen Avatar answered Sep 19 '22 01:09

Frank van Puffelen