https://firebase.google.com/docs/functions/http-events表示rawBody
参数应该存在于req
. 但是,它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();
});
当我运行该函数时,我得到以下结果:
$ firebase emulators:start --only functions
<snip>
i functions: Beginning execution of "endpointName"
>
> undefined
我已经通过 HTTP Toolkit 确认正文实际上是从我的浏览器发送的: