redcartel
redcartel

Reputation: 402

With FlutterFire cloud_functions, are all requests POST?

I am writing an API using Firebase Functions for a Flutter Web application. I cannot see any way to use the FlutterFire cloud_functions.dart package to call my functions using any http method other than 'POST.' Am I missing something?

With this Firebase function:

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'});
    }
});

Is there any way to have code like

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

issue a GET request?

Upvotes: 0

Views: 470

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317487

Requests made to callable functions with the Firebase Functions SDK are always POST and can't be changed. You can see exactly what it's doing if you read over the protocol specification for callable functions.

If you want to use the Firebase Functions SDK to invoke a function, you must also define your function using onCall instead of onRequest, as described in the documentation for callable functions.

If you define your function with onRequest, then you should use a standard HTTP client library to invoke it. The Functions client SDK will not work.

You can't really mix and match onRequest and onCall functions - they have different use cases and implementation details.

Upvotes: 3

Related Questions