![](/img/trans.png)
[英]How can I see the full HTTP request (headers, method, body) in my AWS REST API access logs?
[英]How can I view the body data of an HTTP request in a Firebase Functions JS?
“我正在嘗試查看 Firebase Functions POST 請求中的正文數據,但控制台始終在 Firebase function 日志中返回“未定義”!
問題:如何查看Firebase function中的身體數據來使用?
這是我的代碼:
exports.test = functions.https.onRequest(async (req, res) => {
const id = req.body.id
});
我已經使用但沒有工作:
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
});
我應該從 body.id 獲取 id
根據該線程的回答,您應該使用Content-Type
並將其設置為“application/json”,因為只有這樣您的身體才能像此處所示那樣訪問
您還可以查看請求中的讀取值,其中解釋了application/json
對Content Type
的需求。
如果您檢查 firebase 函數日志,您可以看到沒有從請求中收到傳入正文,因此您的正文內容未定義。
也有可能是 Firebase 函數中的req.body
屬性格式不正確,或者它沒有被 Function 正確解析,所以你可以使用JSON.parse(req.body);
解析請求正文。 所以更新后的代碼看起來像這樣:
exports.test = functions.https.onRequest(async (req, res) => {
const body = JSON.parse(req.body);
const id = body.id;
// ...
});
要么
如果請求主體類型是 application/json 或 application/x-www-form-urlencoded 則可以使用req.body否則使用req.rawBody
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.