Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sending body in POST request in flutter

This is my body for sending data with POST request in postman, it working fine

{
    "title" : "test",
    "description" : "estt",
    "category" : "Health care",
    "imageCover" : "",
    "author" : "",
    "authorImageProfile" : "",
    "dayCount" : "10",
    "goals" : programGoals, --> List<String> programGoals = ["goal1","goal2","goal3"];
}

but, With flutter how can I send this goals array in the json body?
I tried to encode data or make it string but nothing worked.
Please let me know where is the problem and how can I send like this list to server from app.

["cat","dog","rabbit"]

This is my request in flutter (Map jsonMap)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

jsonMap = {
      "title": itemTitle,
      "description": itemDescription,
      "category": category,
      "imageCover": "",
      "author": "",
      "authorImageProfile": "",
      "dayCount": "10",
      "goals": programGoals
    };
final response = await http.post(Uri.parse('$localHost$url'),
          encoding: Encoding.getByName('utf-8'),
          body: jsonMap);

Thanks.

>Solution :

You are missing the encode data.

Whenever we send a request to the server the data should be encoded (Stringified)

So here is the solution –

    jsonMap = {
          "title": itemTitle,
          "description": itemDescription,
          "category": category,
          "imageCover": "",
          "author": "",
          "authorImageProfile": "",
          "dayCount": "10",
          "goals": programGoals
        };
    String data_to_sent = jsonEncode(jsonMap);
    final response = await http.post(Uri.parse('$localHost$url'),
              encoding: Encoding.getByName('utf-8'),
              body: data_to_sent);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading