Receiving Variables in Python Cloud Function from Flutter App

153

Solution 1

I needed to use the request.form.get('uid') to get data from my post request. Hope this helps someone!

Solution 2

You're using the Firebase SDK for Cloud Functions to invoke a callable function, but your python function is just a regular HTTP function. This isn't going to work without implementing the protocol that the SDK uses to communicate with the function. Backend support for callables is only provided for nodejs when deploying with the Firebase CLI.

It will probably be easier if you just make a normal HTTP request from the client instead of writing all the code required for the protocol.

Share:
153
Jonathon King
Author by

Jonathon King

Updated on December 16, 2022

Comments

  • Jonathon King
    Jonathon King over 1 year

    I am trying to send variables from my flutter app through a http trigger to a python script on google cloud functions.

    I can successfully trigger the function but my function does not receive the variable.

    Here is my relevant app code:

      callCloudFunction() async {
        final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
          functionName: 'testPath',
        );
        dynamic resp = await callable.call(<String, dynamic>{
          'uid': uid,
        }).catchError((onError) {
          //Handle your error here if the function failed
    
        });
      }
    

    Here is my relevant python code from cloud functions:

    def main(request):
        name = 'empty'
        request_args = request.args
        request_json = request.get_json(silent=True)
        if request_json and 'uid' in request_json:
            name = request_json['uid']
        elif request_args and 'uid' in request_args:
            name = request_args['uid']
        print(name)
    

    No issues trigger the function. It has other functionality that I did not show here so I can confirm the trigger works. Its just passing that variable "uid" that is not working.

  • Jonathon King
    Jonathon King over 4 years
    Hello! Thanks for the feedback. I am not really sure what you mean. Are you suggesting I change the way I trigger the function from the flutter app?
  • Doug Stevenson
    Doug Stevenson over 4 years
    Yes, use a normal HTTP request, not the Firebase SDK. The Firebase SDK has a special purpose. Click the link to read more.
  • Jonathon King
    Jonathon King over 4 years
    Okay, I think there is an HTTP pluggin for flutter, so I will try that. I appreciate your response. I will post here if there is any luck.
  • Jonathon King
    Jonathon King over 4 years
    Hello, I have added the http library to my app and I am now using that to do a post request. I am able to trigger my function but I still have the issue of not being able to see the variables from the body of the request. Here is my new code (badly formatted because stack overflow has weird comment structure): _makeGetRequest() async { var url = 'us-central1-oto-test-3254b.cloudfunctions.net/otoPath'; var response = await http.post(url, body: {'uid': uid}); } My python code is still the same.
  • Jonathon King
    Jonathon King over 4 years
    I have also tried printing request.data and it is empty.
  • Jonathon King
    Jonathon King over 4 years
    Scratch that I was not using request correctly. This is what I had to do to get data: request.form.get('uid')