[英]rawBody is missing in firebase cloud functions emulator
https://firebase.google.com/docs/functions/http-events表示在req
上應該存在rawBody
參數。 但是,它在我的本地模擬器中是undefined
的。
我究竟做錯了什么?
$ firebase --version
7.8.1
我用於發布二進制數據的客戶端 javascript 代碼:
var buffer = new ArrayBuffer(4);
var view = new DataView(buffer);
function writeUTFBytes(view, offset, string) {
var lng = string.length;
for (var i = 0; i < lng; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
writeUTFBytes(view, 0, 'RIFF');
var blob = new Blob([view]);
fetch('http://localhost:5001/firebase-project-name/us-central1/endpointName', {
method: 'POST',
body: blob
})
我的 index.js firebase 雲函數代碼:
const functions = require('firebase-functions');
exports.endpointName = functions.https.onRequest((req, res) => {
res.set('Access-Control-Allow-Origin', 'http://localhost:3000');
console.log(req.rawBody);
console.log(typeof req.rawBody);
res.end();
});
當我運行 function 時,我得到的是:
$ firebase emulators:start --only functions
<snip>
i functions: Beginning execution of "endpointName"
>
> undefined
我已經通過 HTTP Toolkit 確認正文實際上是從我的瀏覽器發送的:
我發現如果我將內容類型設置為 application/octet-stream 或 audio/wave 那么 rawBody 會突然出現!
fetch(url, {
method: 'POST',
headers: new Headers({
'Content-Type': 'audio/wav',
}),
body: blob
})
但是,我必須修改我的 function 以使 OPTIONS HTTP 請求成功,以便我的瀏覽器詢問是否可以包含 Content-Type Z099FB995346F31C749F6E40DB0F33Z,否則實際請求從未發送:E
res.set('Access-Control-Allow-Origin', 'http://localhost:3000');
if (req.method === 'OPTIONS') {
// Send response to OPTIONS requests
res.set('Access-Control-Allow-Methods', 'OPTIONS, POST');
res.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length');
res.set('Access-Control-Max-Age', '3600');
return res.status(204).send('');
}
但是,我仍然很好奇為什么沒有內容類型 header 就不會發生 rawBody。 至少,可能會有一些文檔介紹發送二進制數據和使用 rawBody 的端到端示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.