jsonflutterdarthttppost

HTTP POST with raw body - Flutter/Dart


I am getting this error

flutter: response body:{"Message":"The request entity's media type 'text/plain' is not supported for this resource."}

which I am getting in post whenever I am changing the body type to text instead of json. How can I solve this. here are the postman screenshots of both successful request and field request are given below

enter image description here enter image description here

Here is the post method

Future<String> sendRequest() async {
      final jsonArray = json.encode(body);
      algo.Encrypted encrypted = Algorithm.encrypt(algorithmKey, jsonArray);
      final encryptedBosy = jsonEncode({'body': encrypted.base64});
      final response = await https.post(
        Uri.parse("$baseUrl/$paymentMethod"),
        headers: {
          "Key": key,
          "secretKey": secretKey,
        },
        body: encryptedBosy,
      );
      return response.body;
    }

Solution

  • Add the content type header to the request.

    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },