Hi I have a http post as

final http.Response response = await client.post(
      'http://someurl/',
        headers: {
          HttpHeaders.contentTypeHeader: "application/json",
          "Content-Type": "application/x-www-form-urlencoded",
          "Authorization": token
        },
        body: {
          "isItTake": false,
          "servisID": 1
        }
    );

But when I try this post method I get "Unhandled Exception: type 'bool' is not a subtype of type 'String' in type cast". I can change the API to expect string but I wonder if there is a work around to send int or boolean. Note that When I send a similar request on postman everything is fine.
Edit:
Postman:
POST /someendpoint/ HTTP/1.1
Host: somehost
Authorization: Token sometoken
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 20582fd0-c980-2d0d-fb2f-3bdd87d767f5 \

{ "isItTake": false, "servisID": 1 }


Solution 1: MrPengueeen

Try sending the request body values as Strings and see if that works. I've faced this issue before with type mismatch with the request bodies of http requests and I'm not quite sure as to why it throws exceptions like that even though the documentation for the api clearly specifies the type for each value in the request body. Try this:

final http.Response response = await client.post(
      'http://someurl/',
        headers: {
          HttpHeaders.contentTypeHeader: "application/json",
          "Content-Type": "application/x-www-form-urlencoded",
          "Authorization": token
        },
        body: {
          "isItTake": 'false',
          "servisID": '1'
        }
    );

Or in case if you have your values in some bool and int variables:

final http.Response response = await client.post(
      'http://someurl/',
        headers: {
          HttpHeaders.contentTypeHeader: "application/json",
          "Content-Type": "application/x-www-form-urlencoded",
          "Authorization": token
        },
        body: {
          "isItTake": isItTake.toString(),
          "servisID": servisID.toString()
        }
    );


Solution 2: jignesh.world

Use String encoded = json.encode(theMap); then post encoded. If you need a particular character encoding (e.g. utf-8) then further encode the string using utf8.encode(encoded) and post the resulting byte array. (The second step should be unnecessary for utf-8 as I think that is the default.)

It's worth considering what the 3 variants do:

  • List<int> - sends an opaque byte array
  • String encodes the string into bytes using a character encoding - and sends the byte array
  • Map<String, String> - encodes the string key/value pairs in x-www-form-urlencoded and sends that.

If you want to send more complex data then you need to convert it into one of the above (and the server needs to know how to decode it). That's where the content-type header is useful. Ultimately, the server receives a byte array and converts it back into, for example, a string, or some json, or a set of form fields, or an image. It knows how to do this based on the header and any specified encoding.

Complete Credit: Source