firebase cloud function post request body

10,448

Solution 1

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

Solution 2

I found the problem: I added 'Content-Type': 'multipart/form-data' in the headers when I post the request from my Angular form.

Share:
10,448
Giannis Savvidis
Author by

Giannis Savvidis

Updated on June 21, 2022

Comments

  • Giannis Savvidis
    Giannis Savvidis almost 2 years

    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?