简体   繁体   English

如何在 Firebase Functions JS 中查看 HTTP 请求的 body 数据?

[英]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! “我正在尝试查看 Firebase Functions POST 请求中的正文数据,但控制台始终在 Firebase function 日志中返回“未定义”!

Question: How can I view the body data in a Firebase function to use it?问题:如何查看Firebase function中的身体数据来使用?

Request:要求: 在此处输入图像描述

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我应该从 body.id 获取 id

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根据线程的回答,您应该使用Content-Type并将其设置为“application/json”,因为只有这样您的身体才能像此处所示那样访问

Also you can check out Read values from the request which explains the need of application/json on Content Type .您还可以查看请求中的读取值,其中解释了application/jsonContent 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.如果您检查 firebase 函数日志,您可以看到没有从请求中收到传入正文,因此您的正文内容未定义。

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);也有可能是 Firebase 函数中的req.body属性格式不正确,或者它没有被 Function 正确解析,所以你可以使用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您可以使用正文解析器中间件并将正文强制解析为 JSON ,如此答案所示

req.body can be used if the request body type is application/json or application/x-www-form-urlencoded otherwise use req.rawBody如果请求主体类型是 application/json 或 application/x-www-form-urlencoded 则可以使用req.body否则使用req.rawBody

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的 AWS REST API 访问日志中查看完整的 HTTP 请求(标头、方法、正文)? - How can I see the full HTTP request (headers, method, body) in my AWS REST API access logs? 我可以在 Cloud Functions Http 触发器中创建 Firebase 身份验证用户吗? - Can I create a user on Firebase Authentication in Cloud Functions Http Trigger? 在 Firebase Cloud Functions 上使用 ApolloServer 时,我会为每个 http 轮询请求付费吗? - Do I get billed for each http polling request when using ApolloServer on Firebase Cloud Functions? 如何发送 HTTP 请求以在 firebase 中发送通知? - how to i send HTTP request for sending notification in firebase? HTTP 带有请求正文的 POST - HTTP POST with request body 如何从 Cloud Functions 访问和操作 Firebase 数据库? - How can I reach and manipulate Firebase Database from Cloud Functions? 如何将 Firebase 参数化配置与 Typescript 一起用于 Cloud Functions? - How can I use Firebase Parameterized configuration with Typescript for Cloud Functions? Azure functions v3 /HTTP trigger functions: Limiting the request body and URL 大小 - Azure functions v3 /HTTP trigger functions : Limiting the request body and URL size 如何检查 data_URL 是否返回视频图像? - Firebase & Next.js/反应 - How can I check if the data_URL is returning an image of video ? - Firebase & Next.js/React 从 firebase 存储下载文件与 HTTP 请求任何人都可以下载 - Download file from firebase storage with HTTP request that anyone can download
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM