24

I'm calling post API, and sometimes I get the response from the server and sometimes I receive the exception:

Connection closed while receiving data.

Request is same in both cases, and according to backend server logs, a response is sent but I didn't receive it. I have this issue both in simulator and actual device.

try {
  final result =
      await http.post(url, body: encodedBody, headers: apiHeader);
  Map<String, dynamic> response = json.decode(result.body);

  print("Response: $response");

  return response;
} catch (error) {
  Map<String, dynamic> response = Map<String, dynamic>();
  response['success'] = false;
  response['message'] = error;
  return response;
}
6
  • what are the headers are you sending? does it contain a keep alive header?? please post the headers Commented Jul 24, 2020 at 5:47
  • i was not sending keep alive in the headers, sending this resolves my issue. Thanks a lot Commented Jul 24, 2020 at 6:20
  • I'll post this as a answer to your question Commented Jul 24, 2020 at 6:23
  • it might help the future answer seekers Commented Jul 24, 2020 at 6:29
  • 1
    @LucasAschenbach i just added this Connection header this.apiHeader['Connection'] = "keep-alive"; Commented Apr 29, 2021 at 17:23

5 Answers 5

13

Keep-Alive header in the headers of your request might be missing, please check with APIs required headers

Sign up to request clarification or add additional context in comments.

6 Comments

bro, I am still facing this issue and I also try adding this Keep-Alive in the header section still don't work! if you can help please
Check if you're correctly adding it
yes i am adding it ina correct way 'Connection': 'Keep-Alive', 'connection': 'keep-alive', i tried in both ways lower case and upercase
I also haven't had any success with this header. I'm really not sure what to do. My connection just randomly fail.
need more details
|
5

In my case the server was closing the connection when there was no 'Accept-Encoding' header, even with 'Connection': 'Keep-Alive'. The backend was developed in Laravel, but I don't have much information. I added to the header 'Accept-Encoding': 'gzip, deflate, br' and it worked as it should. Don't ask me for an explanation, because I don't have it

Comments

4

I had the issue with a get request, sometimes I get the response from the server and sometimes I receive the exception Connection closed while receiving data on same button click event. Then I founded that the number of records of particular table in database is quite large. My suggested solution is to use pagination in the backend server.

Comments

1

It depends on the server or Rest API you are trying to call.

Sometimes it is a matter of double checking you are using a valid and appropriate URL endpoint.

Another possibility is that it is not correctly receiving authentication at the header. I had that problem once and it got solved authenticating trough the url as endpoints. Try researching if the server or rest api supports other authentication options as this last one mentioned.

Comments

1

I have resolved this, by using this GitHub issue https://github.com/flutter/flutter/issues/22951

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.