Flutter Bool Data in Post Object

Viewed 578

I have a post object. Like Below.

{
       "facilities": value.join(","),
       "start_date": startDate.toString().substring(0, 10),
       "end_date": endDate.toString().substring(0, 10),
       "over_ratio": false,
       "total": true
}

I send this object in body, but server send me 404 message. but When I write my bool data like "over_ratio": "false", "total": "true", server understands this bool data wrong.

var response = await http.post(url, body: body);
    if (response.statusCode == 200) {
     print(response.body);
    }

I need to send this bodys bool data like:

{
       "facilities": value.join(","),
       "start_date": startDate.toString().substring(0, 10),
       "end_date": endDate.toString().substring(0, 10),
       "over_ratio": false,
       "total": true
}

I also tried like, but same problem occurs:

json.encode({
       "facilities": value.join(","),
       "start_date": startDate.toString().substring(0, 10),
       "end_date": endDate.toString().substring(0, 10),
       "over_ratio": false,
       "total": true
});

How can I send this post correctly ? Thanks.

2 Answers

Have you tried to post data with jsonEncode(body)

Hope it will work.

I solve my problem like this.

Map<String, String> get headers =>
      {"Authorization": "Bearer $token", "Content-Type": "application/json"};

My old header data like this below.

Map<String, String> get headers =>
          {"Authorization": "Bearer $token"}

And I also did my json data like below with jsonEncode().

jsonEncode({
              "facilities": value.join(","),
              "start_date": startDate.toString().substring(0, 10),
              "end_date": endDate.toString().substring(0, 10),
              "over_ratio": false,
              "total": true
            })
Related