0

I am trying to send a json object to an api end point. But I get the following:

I / flutter(28184): 200
I / flutter(28184): {"error": "the JSON object must be str, not 'bytes'"}

I have tried the following:

Future<http.Response> sendPost() async{
    var post = {
      "profile_id" : "${_profile_id}",
      "profile_name" : "${_profile_Name}",
      "profile_country" : "${_profile_country}",
      "post_id" : "${current_post_id}",
      "post_image_id" : "${current_post_image}",
      "post_desc" : "La La La La La La",
      "post_likes" : "${post_likes}",
      "post_timestamp" : "05-06-2019 22:34",
      "post_comments" : [],
      "post_comments_profile_name" : [],
      "post_comments_profile_image_id" : [],
      "post_comments_timestamp" : []
    };


    var body = utf8.encode(json.encode(post));

    var addPost = await http.post(
      url,
      headers: {"content-type" : "application/json"},
      body: body

      );

      print("${addPost.statusCode}");
      print("${addPost.body}");
      return addPost;
  }

Also when you answer remember that there should be empty arrays inside this Map.

halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

0

No need to convert the map:

var body = utf8.encode(json.encode(post)); // delete this

Just send the map to the body like this:

var addPost = await http.post(
    url,
    body: post
);
Robert
  • 7,394
  • 40
  • 45
  • 64
0

In order to send the JSON into body, you should stringing the JSON.

var body = JSON.stringify(post);

I think no need to encode.

0

Do not convert the map by using utf8.encode(). Since the JSON object must be a string and not bytes. Directly send the map :

var addPost = await http.post(
    url,
    body: post
);

I understand from the comment that you want to retrive the string array in future. Do not worry that can be easily achieved :

Link for the same: Flutter how to convert String to List<String>

Kshitij dhyani
  • 591
  • 1
  • 6
  • 14