你可以查看 Firebase Functions JS 中的 HTTP 请求体数据如何处理。

huangapple go评论62阅读模式
英文:

How can I view the body data of an HTTP request in a Firebase Functions JS?

问题

"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 :
你可以查看 Firebase Functions JS 中的 HTTP 请求体数据如何处理。

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 get 'id' from 'body.id'.

英文:

"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 :
你可以查看 Firebase Functions JS 中的 HTTP 请求体数据如何处理。

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

答案1

得分: 1

根据这个线程的回答,您应该使用Content-Type并将其设置为"application/json",只有这样您的请求主体才能像这里所示一样可访问。

此外,您可以查看从请求中读取值,该文档解释了Content Type上使用application/json的必要性。

如果您查看 Firebase 函数日志,您会看到请求中没有接收到主体,因此您的主体内容是未定义的。

还有一种可能性是 Firebase 函数中的req.body属性未正确格式化,或者如果函数没有正确解析它,您可以使用JSON.parse(req.body);强制解析请求主体。
因此,更新后的代码将如下所示:

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

或者

您可以使用 body 解析中间件,并强制将主体解析为 JSON,如此回答所示。

英文:

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

答案2

得分: 0

req.body可以在请求体类型为application/json或application/x-www-form-urlencoded时使用,否则使用req.rawBody

英文:

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

huangapple
  • 本文由 发表于 2023年1月3日 16:31:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/74990788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定