HttpException: Connection closed before full header was received

Viewed 8746

I am trying to make post call as the code bellow shows.

  Map<String, String> header = {"Content-Type": "application/json"};
  Map<String, String> body = {
    "client_id": "xxxx",
    "client_secret": "XXXX",
    "password": "password",
    "username": "username",
  };

  Future<Post> callForFirstToken() async {
    final response = await http.post(url, headers: header, body: body);
    return response.statusCode == 200
      ? Post.fromJson(json.decode(response.body))
      : throw Exception('Failed to load ...');}

But, I have got this error:

Error connecting to the service protocol: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:60556/sK_cPlYMcGQ=/ws
5 Answers

I have replaced the header to as bellow shows and works

{"HttpHeaders.contentTypeHeader": "application/json"}

I think I had the same issue, but I'm not sure how I solved. I think it was adding this to the header: ..headers["Content-Type"] = "application/json"

I am not entirely sure, but I have seen this before in my case. This error has likely nothing to do with your actual network call which your posted. The ip adress you can see 127.0.0.1 is the one of your local machine which is being used during debugging.

This means that debugging is not possible with the device. You might want to restart and reconnect the IDE and/or phone and turn of any VPN program. For me this worked.

You might also want to set the response.body to a Text widget to check if the actual network call was successful.

The problem is telling you that the connection closed because the appropriate response from the server was not received. This means somewhere in your server code, it's stuck due to exception or stuck due to heavy intensive work, etc. You should encapsulate your server code in try-catch and see why your code is stuck. My guess is, you forgot to send back data to the client after post request, and your client is still waiting for that data.

Related