Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

error when triggering Cloud Run from Flutter

I need to send a post request from Flutter to activate a Cloud Run service.
The service app code for the POST method in the back end is as follow:
 

 

app = Flask(__name__)
@app.route("/", methods=["POST"])
def index():
    print("I AM IN INDEX")
    print("1")
    print(type(request))
    print("2")
    envelope = request.get_json()
    print("3")

 

 

 where the several prints are meant to understand where the flow fails.
 
In the front end, if I use the following script:

 

var http_request = jsonEncode({
            "param1": "abc",
            "param2": "xyz"
        });

var uri = 'https://service-pphjhss-ez.a.run.app';

final res = await http.post(
             Uri.parse(uri),
             headers: {'Content-Type': 'application/json'},
             body: http_request);

 

In the front end I get an Error: XMLHttpRequest error. 
In the back end, a message OPTIONS 200 is generated in the Cloud Run logs (which seems correct to me). The problem is that the control flow does not enter in the  "def index():" meaning that the message "I AM IN INDEX" is nto printed out in the logs.
 
If I remove  headers: {'Content-Type': 'application/json'} in the request, in the frontend, the flow gets into the index function but the flow stops in line 8, in the get_json().
 
Observe that 

 

export URL=https://service-pphjhss-ez.a.run.app
curl -X POST -H "content-type: application/json" \
     -d '{
            "param1": "abc",
            "param2": "xyz"
        )' -w "\n" $URL

 

 works fine!
 
So it is obviously a problem in the request in Flutter. But I cannot understand what;'s wrong with it
2 2 326
2 REPLIES 2

@magenti same exact issue here. Did you solve it?

I am also facing the same error, did you fix it?