Get Request works in postman but not work in flutter giving 400 error

91 Views Asked by At

I am sending a get request with JSON body and bearer token it is working fine in Postman but it is not working in Flutter code and gives 400 bad requests with this response:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-2460b88e437fb2021b6249f0a3d0abf0-3f2e0cef2e5fefa3-00",
    "errors": {
        "$": [
            "The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0."
        ],
        "signIn": [
            "The signIn field is required."
        ]
    }
}

Here is my flutter code:

final body = userData.toJsonLogin();

var headersList = {
  'Accept': '*/*',
  'Authorization': 'Bearer $token',
  'Content-Type': 'application/json',
};

var request =
    http.Request('GET', Uri.parse('https://192.168.0.128/api/Profile'));

request.body = body;

request.headers.addAll(headersList);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
} else {
  print(response.reasonPhrase);
}

How can I solve this problem the API is provided by my senior and I can't change it.

1

There are 1 best solutions below

1
Muddassar On

I think issue is in the header your header map

var headersList = {
 'Accept': '*/*',
 'Authorization': 'Bearer $token',
 'Content-Type': 'application/json',  
};

And correct one is

var headersList = {
 'Accept': '*/*',
 'authorization': 'Bearer $token', // Authorization -> authorization
 'Content-Type': 'application/json',  
};

let me know if the issue resolved.