0

"I'm trying to view the body data in a Firebase Functions POST request, but the console always returns "undefined" in the Firebase function logs!

Question: How can I view the body data in a Firebase function to use it?

Request : enter image description here

Here My Code :

exports.test = functions.https.onRequest(async (req, res) => {
 const id = req.body.id

});

I allready used but not working :

  req.on('data', (chunk) => {
    // The chunk is a Buffer object containing a chunk of the request body data
    body += chunk;
  }).on('end', () => {
    // The request body data has been fully received, and the `body` variable contains the complete request body
  });

i should to get id from body.id

Khalifa Alkhatri
  • 244
  • 1
  • 4
  • 20
  • Please add the request that you are calling the function with, including any relevant headers. – samthecodingman Jan 03 '23 at 10:06
  • This [other answer thread](https://stackoverflow.com/a/64746618/3068190) may also be of use. – samthecodingman Jan 03 '23 at 10:12
  • updated. I still dont know – Khalifa Alkhatri Jan 03 '23 at 10:49
  • Voting to reopen as OP is using a Content Type of `multipart/form-data` not `application/json`. – samthecodingman Jan 03 '23 at 16:40
  • Khalifa, in the mean time, you should add `busboy` as a dependency to your project and then feed it `req.rawBody`. This is because `multipart/form-data` is not one of the content types handled automatically by the [Functions Framework](https://firebase.google.com/docs/functions/http-events#read_values_from_the_request). There are a number of examples using it with Cloud Functions here on SO. What data is your function uploading? (images, videos, text, etc.) – samthecodingman Jan 03 '23 at 16:44
  • can you take a look at mu answer ? – Rohit Kharche Jan 05 '23 at 06:45

2 Answers2

1

As per this thread’s answer you should use Content-Type and make it to "application/json" as only then your body will be accessible like as shown in here

Also you can check out Read values from the request which explains the need of application/json on Content Type.

If you check the firebase functions logs you can see there is no incoming body received from the request and because of that your body content’s are undefined.

There is also a possibility that the req.body property in a Firebase Functions is not properly formatted or if it is not properly parsed by the Function, so you can force it to parse using JSON.parse(req.body); to parse the request body. So the updated code will something look like:

exports.test = functions.https.onRequest(async (req, res) => {
  const body = JSON.parse(req.body);
  const id = body.id;
  // ...
});

OR

You can use the body parser middleware and force parse the body as JSON as shown in this answer

Rohit Kharche
  • 2,541
  • 1
  • 2
  • 13
  • Contacting Firebase Support for this problem isn't suitable. My mention of contacting them on [the linked answer](https://stackoverflow.com/a/69733262/20239914) was because the built-in Framework was crashing without letting the user's code have the opportunity to handle the error if the body could not be parsed by the built-in JSON parser. – samthecodingman Jan 03 '23 at 16:39
  • @samthecodingman Edited the answer can you have a look at it again ? – Rohit Kharche Jan 04 '23 at 07:34
0

req.body can be used if the request body type is application/json or application/x-www-form-urlencoded otherwise use req.rawBody

Muhammad Yasin
  • 418
  • 4
  • 11