Samuel Alves
Samuel Alves

Reputation: 3

Error when i try to send a Json from my app

I try to send a json from my Flutter app to my Flask Backend app, but the json is not being sent.

My Dart Code:

    Map<String, dynamic> jsonMap = {
      "Title": "Titulo da todo",
      "Description": "Descrição da todo",
    };

    final response = await client.post("http://10.0.2.2:5000/todo", body: jsonEncode(jsonMap);

    final Map result = jsonDecode(response.body);
    print(result);
  }

the result.body is always {'error' : 'invalid'}

Python code

def create_todo():
    try:
        json = request.get_json()
        print("Json: ", json)
        #return {'ok' : 'ok post'}
        return jsonify(TodoService().create(json))
    except:
        return {'error' : 'invalid'}
Json:  None
127.0.0.1 - - [12/Nov/2019 16:27:00] "POST /todo HTTP/1.1" 200 -

Upvotes: 0

Views: 55

Answers (1)

Richard Heap
Richard Heap

Reputation: 51750

From the flask documentation:

By default this function will only load the json data if the mimetype is application/json but this can be overridden by the force parameter.

Make sure you add the content-type header:

  await client.post(
    'http://10.0.2.2:5000/todo',
    headers: {'content-type': 'application/json;charset=utf-8'},
    body: utf8.encode(json.encode(jsonMap)),
  );

Upvotes: 1

Related Questions