Laila Mattar
Laila Mattar

Reputation: 361

How to fix exception : Unhandled Exception: Connection closed before full header was received

I send request and i have an exception this is model class:

 class User{
  String mobileId , username;

  User({this.username,this.mobileId});

  factory User.fromJson(Map<String, dynamic> json){
    return User(
      username: json['userName'],
      mobileId: json['mobileId'],
    );
  }
}

this is the function which i send request

Future<User> sendRequest(String mobileId , String username)async{
  http.Response response = await http.post('http://94.237.88.194:8080/player',
    body:jsonEncode(<String,String>{
      'userName'  : username,
      'mobileId' : mobileId,
    }),
  );
  print(response.statusCode);

  return User.fromJson(jsonDecode(response.body));
}

Upvotes: 2

Views: 297

Answers (1)

Laila Mattar
Laila Mattar

Reputation: 361

I have found the solution I added header to post function

Map<String, String> header = {"Content-Type": "application/json"};

and the function become:

Future<User> sendRequest(String mobileId , String username)async{
  print('5');
  Map<String, String> header = {"Content-Type": "application/json"};

  http.Response response = await http.post('http://94.237.88.194:8080/player',headers: header,
    body:jsonEncode(<String,String>{
      'userName'  : username,
      'mobileId' : mobileId,
    }),
  );
  print('4');
  print(response.statusCode);
  print(response.body);

  return User.fromJson(jsonDecode(response.body));
}

Upvotes: 2

Related Questions