Home > Back-end >  ContentType issue in headers in POST request (Flutter)
ContentType issue in headers in POST request (Flutter)

Time:09-28

0

I have a problem probably with the parameter ContentType in POST method in Dart. I'm trying to make API call to change the value of expires_in in body (default value:3600). But it seems like Android cannot read Content Type from headers, because I have the same result in Android Studio and Postman when I inactive the ContentType header in Postman. When this header is active in Postman it returns the expires_in changed value properly. Where could be a mistake? In passing headers?

Dart code:

Future<http.Response> getPrivateKeyResponse(Uri url) async {
    var customerKey = "***";
    var customerSecret = "***";
    var encode = base64Encode(utf8.encode("$customerKey:$customerSecret"));

    return await http.post(
      Uri.parse('https://api.dolby.io/v1/auth/token'),
      headers: <String, String>{
        "Authorization":"Basic $encode",
        "Content-Type": "application/x-www-form-urlencoded"
      },
      body: jsonEncode(<String, dynamic>{
        "grant_type":"client_credentials",
        "expires_in":7200
      }),
    );
  }

Screenshot from Postman result with changed value to 7200 with active ContentType header (correct result):

Screenshot from Postman result with changed value to 7200 with inactive ContentType header (wrong result):enter image description hereenter image description here

And with inactive ContentType header it returns the same result as Android Studio with code above (wrong result): enter image description here

So it brings me to thinking that passing this ContentType in headers not working properly. Do you have any suggestions? Or maybe the code is wrong on the Dart side?

CodePudding user response:

You can check postman to give you the exact code to generate the same working request in dart. There is a code button (probably will be on the top right side) to show you the code in different languages, choose dart and check if you're missing anything.

enter image description here

  • Related