Skip to content

HttpClientRequest.contentLength incorrectly transmitted when using contentLength and header #30842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
donny-dont opened this issue Sep 20, 2017 · 1 comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io

Comments

@donny-dont
Copy link

This is a simple reproduction of the issue dart-lang/http#110 is meant to fix

Given the following code

Future post() async {
  Map jsonData = {
    'name': 'Han Solo',
    'job': 'reluctant hero',
    'BFF': 'Chewbacca',
    'ship': 'Millennium Falcon',
    'weakness': 'smuggling debts'
  };

  var request = await new HttpClient()
      .openUrl('POST', Uri.parse('https://httpbin.org/post'));

  final encoded = JSON.encode(jsonData);
  request.contentLength = encoded.length;
  request.headers.set('content-length', encoded.length);
  request.write(encoded);

  HttpClientResponse response = await request.close();
  await for (var contents in response.transform(UTF8.decoder)) {
    print(contents);
  }
}

The server returns

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Connection": "close", 
    "Content-Length": "0", 
    "Host": "httpbin.org", 
    "User-Agent": "Dart/2.0 (dart:io)"
  }, 
  "json": null, 
  "origin": "173.230.196.25", 
  "url": "https://httpbin.org/post"
}

However if request.contentLength is not set or if the call to request.headers.set('content-length', encoded.length) is moved above the setter the following response is received from the server.

{
  "args": {}, 
  "data": "{\"name\":\"Han Solo\",\"job\":\"reluctant hero\",\"BFF\":\"Chewbacca\",\"ship\":\"Millennium Falcon\",\"weakness\":\"smuggling debts\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Connection": "close", 
    "Content-Length": "116", 
    "Host": "httpbin.org", 
    "User-Agent": "Dart/2.0 (dart:io)"
  }, 
  "json": {
    "BFF": "Chewbacca", 
    "job": "reluctant hero", 
    "name": "Han Solo", 
    "ship": "Millennium Falcon", 
    "weakness": "smuggling debts"
  }, 
  "origin": "173.230.196.25", 
  "url": "https://httpbin.org/post"
}
@vsmenon vsmenon added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label Sep 25, 2017
@robinloxley1
Copy link

It happens on 0.11.3+16 as well.
The workaround is to let library to decide content-length, without setting it to headers.

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. and removed area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io
Projects
None yet
Development

No branches or pull requests

4 participants