【问题标题】:With FlutterFire cloud_functions, are all requests POST?使用 FlutterFire cloud_functions,所有请求都是 POST 吗?
【发布时间】:2021-03-10 09:03:41
【问题描述】:

我正在为 Flutter Web 应用程序使用 Firebase 函数编写 API。我看不到任何使用 FlutterFire cloud_functions.dart 包使用“POST”以外的任何 http 方法调用我的函数的方法。我错过了什么吗?

使用这个 Firebase 功能:

export const hello = functions.https.onRequest(async (req, res) => {
    cors(req, res);
    functions.logger.info(req.method);
    switch (req.method.toLowerCase()) {
        case 'get':
            res.status(200).send({'data': 'get', 'error': false });
        break;
        case 'post':
            res.status(201).send();
        break;
        case 'options':
            res.status(200).send();
        break;
        default:
            res.status(405).send({error: true, data: {}, message: 'method not allowed'});
    }
});

有什么办法可以得到类似的代码

HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('hello');
HttpsCallableResult result = await callable();
print(result.data);

发出 GET 请求?

【问题讨论】:

    标签: firebase google-cloud-functions flutter-web


    【解决方案1】:

    使用 Firebase Functions SDK 向可调用函数发出的请求始终为 POST 且无法更改。如果您阅读protocol specification for callable functions,您可以确切地看到它在做什么。

    如果要使用 Firebase Functions SDK 调用函数,还必须使用 onCall 而不是 onRequest 定义函数,如 documentation for callable functions 中所述。

    如果您使用onRequest 定义您的函数,那么您应该使用标准的HTTP 客户端库来调用它。 Functions 客户端 SDK 将不起作用。

    你不能真正混合和匹配onRequestonCall 函数 - 它们有不同的用例和实现细节。

    【讨论】:

    • 谢谢。我最终将 onCall 用于直接从我的客户端代码调用的所有函数,并将 onRequest 用于外部 API 所需的 webhook。 onCall 与编写 REST API 不同,但最终会更省力。
    猜你喜欢
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多