Search code examples
flutterhttpdartpostpostman

POST call works on Postman but not on Flutter


  Future<String> loginUser(User user) async {
var body = jsonEncode({
  'strlogin': user.email,
});
Response response = await post(SeguriSignAPIURL.loginUser,
    headers: headers,
    body: body);

if (response.statusCode == 200) {
  var decode = jsonDecode(response.body);
  return decode['token'];
} else {
  print(response.reasonPhrase);
  return '';
}

Hey! When I make this Post call on Postman I get a result, however when I run this code on flutter I get a 400 error. My headers:

  final headers = {
'Content-Type': 'application/json; charset=UTF-8',
"Accept": "application/json",

};

Postman: enter image description here


Solution

  • In Postman you do the request using form-data, but in your Flutter code, you pass json-encoded body.

    You either need to use form-data in Flutter or update your server code to accept json content type.