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)
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);