我的本地函数运行良好
firebase serve --only functions
但是一旦将它部署到云中,我就无法使用 postman 向它发出相同的 get 请求。我在 stackdriver 上收到以下错误:
Unexpected token u in JSON at position 0 at JSON.parse
,我的请求返回以下内容:400. That’s an error.Your client has issued a malformed or illegal request. That’s all we know.
我在本地和 firebase 中发送的数据是
GET
请求类型 application/json
有一个 body :{
"data": {
"Celebrity_A": "Brad Pitt",
"Celebrity_B": "Angelina Jolie"
}
}
与本地相比,firebase 功能期望远程有什么要求?
下面是我的函数的开始:
// Main index.ts
exports.funct = functions.https.onRequest(functexp)
// functexp file
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as _request from 'request';
const serviceAccount = require('./credentials.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();
export function functexp(request, response) {
console.log(`request`);
console.log(request);
let celebName_A = null;
let celebName_B = null;
if(request !== undefined){
celebName_A = request.body.data['Celebrity_A'];
celebName_B = request.body.data['Celebrity_B'];
console.log(`celebA is ${celebName_A}`)
} etc...
}
最佳答案
尝试将您的请求发送为 POST
方法,你看不到GET
经常用 body 请求,这就是为什么 POST
更安全,它从不缓存,最重要的是没有大小限制,也许这就是导致您的 GET
的原因。请求不起作用。
希望能帮助到你。
关于Firebase 函数从 get 请求返回 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53729414/