2
votes

while calling a post API on flutter via http.dart i am getting following error:

HttpException: Connection closed before full header was received, uri = http://192.168.3.218:12225/resourcabc/subresourceXYZ

Why is it so when postman is giving correct response. I have seen this issue being asked before too. Also, i am debugging on a physical device: samsung

Here is the code

  Future<Map> post(
String path, {
String token,
dynamic body,
bool parseResponse: false,
isFormData: false,
isUseBaseURL: false,
isEncoded: false,
  }) async {
Uri uri;
uri =
    isUseBaseURL ? Uri.parse('${Paths.baseUrl}/$path') : Uri.parse('$path');

print(uri.toString());
print(body.toString());

var request =
    await client.postUrl(uri).timeout(const Duration(seconds: 30));

if (token != null) {
  request.headers.add(HttpHeaders.authorizationHeader, 'Bearer $token');
} else {
  print('token is null');
  request.headers.add("X-Consumer-Custom-ID", "96");
}

if (body != null) {
  if (isFormData) {
    request
      ..headers.contentType = new ContentType(
          'application', 'x-www-form-urlencoded',
          charset: 'utf-8')
      ..write(body);
  } else {
    request
      ..headers.contentType = ContentType.json
      ..write(isEncoded ? body : json.encode(body));
  }
}
print(json.encode(body));
print('Sending data');
var response = await request.close();
print(response.statusCode);
Map responseMap = await _extractJson(response);
_checkAndThrowError(response, responseMap);
return responseMap;

}

1
did you find any solution to this? - Benjamin Smith
@Benjamin Smith no i havent, i just moved the API to a working mock service and that resolvedfor me - Sana.91
the problem is that the error is not sufficient to analyze the issue - Sana.91
Anyone have found any solution? I'm facing same with my production applications. - Purvik Rana
In my case the call was to an https address using Internet Information Services as backend, in the SSL settings of the website in IIS i had mistakenly set "Client certificates: Accept" instead of "Client certificates: Ignore", setting "Ignore" solved the problem. - Fabio Pagano

1 Answers

0
votes

just replace https with htttp solved the problem .replaceAll('https', 'http')

source: https://github.com/flutter/flutter/issues/25107