Murilo Haas
Murilo Haas

Reputation: 13

Error occuring when calling http post request. ClientException (Failed to Parse HTTP, 67 does not match 13)

I'm trying to do this simple http request, and I always get this error, I have already tried with dio package too.

Error.png

The rest api is from a client, but I have tried the same request by the postman and it works. The code:

try {
  dynamic response = await http.post(
    "https://api/signup",
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode({}),
  );

  return response;
} catch (e) {
  print(e);
}

The flutter doctor.

[√] Flutter (Channel stable, 1.22.4, on Microsoft Windows [versão 10.0.19041.630], locale pt-BR)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[!] Android Studio (version 3.4)
    X Dart plugin not installed; this adds Dart specific functionality.
    X Unable to determine bundled Java version.
[!] Android Studio (version 4.1.0)
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.      
[√] VS Code, 64-bit edition (version 1.51.1)
[!] Connected device
    ! No devices available

! Doctor found issues in 3 categories.

Upvotes: 1

Views: 1329

Answers (1)

Rodrigo Dietze
Rodrigo Dietze

Reputation: 26

I'm commenting here the solution we found for this problem so that anyone facing similar issue can solve it. The problem is dart http package does not FULLY support HTTP 1.1. In HTTP 1.1 receiving Transfer-encoding: chunked is permissive whereas it has not been the case in http 1.0. What that means? When you make a http 1.1 request, you may receive data not at once, but in chunks. In that way Content-length will be null, and the http client should concatenate all the chunks and calculate content-length dynamically. And that is what this package was not doing. It would get the header Transfer-encoding: chunked and ignore it, it would obviously be followed by a null Content-length header, raising the ClientException where the content-length would not match the value of the first chucnk 67 does not match 13. So the solution was either: - Add support to http 1.1 (including chunked responses) to this package OR hack it and enforce this client downgrading to http 1.0 (where there is no chunked responses) or ..... Do what we did since we had access to the web server and add a "chunked_transfer_encoding off;" inside the location ~ .php { block in nginx. If you dont use nginx, you probably have a similar option available for your webserver.

Upvotes: 1

Related Questions