Http: MultipartRequest does not contain Content-Type header

Created on 27 Dec 2019  路  10Comments  路  Source: dart-lang/http

Content-Type header is not included in request when sending MultipartRequest.

import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart' as mime;

void main() {
  _run();
}

Future<void> _run() async {
  final uri = Uri.parse("http://localhost:8080/aaa");
  final req = http.MultipartRequest("POST", uri);

  final files = <http.MultipartFile>[];
  files.add(await http.MultipartFile.fromPath(
    "textfile",
    "./main.dart",
    contentType: mime.MediaType("text", "plain"),
  ));

  final sres = await req.send();
  final res = await http.Response.fromStream(sres);
  print(res.body);
}

Request dump by server:

2019/12/27 14:58:54 POST /aaa HTTP/1.1
Host: localhost:8080
Accept-Encoding: gzip
Content-Length: 76
User-Agent: Dart/2.7 (dart:io)

--dart-http-boundary-GcF.kzHYrmQE0Gpl3ByUUa.WC6SNn7po0k+F3r3iFULEmDsBZ3W--

env

  • Dart VM version: 2.7.0 (Mon Dec 2 20:10:59 2019 +0100) on "macos_x64"
  • http: 0.12.0+3

additional

With http: 0.12.0+2, Content-Type header exists:

2019/12/27 14:55:36 POST /aaa HTTP/1.1
Host: localhost:8080
Accept-Encoding: gzip
Content-Length: 76
Content-Type: multipart/form-data; boundary=dart-http-boundary-Wq2xuOJiK-ZZ3QFowkbQWC0-3Dv+167P3sKF2eqOcxb3d4oXm7d
User-Agent: Dart/2.7 (dart:io)

--dart-http-boundary-Wq2xuOJiK-ZZ3QFowkbQWC0-3Dv+167P3sKF2eqOcxb3d4oXm7d--

Most helpful comment

I have same issue. Workaround is to hardcode http version in pubspec.yaml:

dependencies:
  http: 0.12.0+2

All 10 comments

I expect behavior that includes multipart/form-data in Content-Type like 0.12.0+2.

Facing the same issue with latest flutter upgrade. Used to work fine till 1.9.1 hotfix 6 but with 1.12 update can't uploaf files. Shows null as soon as went to server.

I have same issue. Workaround is to hardcode http version in pubspec.yaml:

dependencies:
  http: 0.12.0+2

Facing the same issue. downgrade to the specific version fixed it

I would expect MultiPartRequest to always get a 'content-type' header, that happens here: https://github.com/dart-lang/http/blob/180c77ae3ef202072de54f141d210cf4e277c0e7/lib/src/multipart_request.dart#L93

We fixed a bunch of lints between these versions, I'll see if anything looks suspicious.

https://github.com/dart-lang/http/compare/0.12.0+2...0.12.0+3

Ah, I have a hunch it might be this change: https://github.com/dart-lang/http/pull/328/files#diff-1bcc559d7868729b92bc2b01dec3c418R90

I suspect what happened here is that there is an extra delay before we set that header - it probably doesn't get set until the stream is listened to, and by that time the headers were probably already read.

Shortly after creating this issue, I created a PR https://github.com/dart-lang/http/pull/352/files that fixed this issue.

I was disappointed that it was not reviewed and a similar fix was created after my PR.

I was disappointed that it was not reviewed and a similar fix was created after my PR.

Sorry for the missed review - we are still working out better processes so that these types of things don't fall through the cracks without relying on a single point of failure.

I'm glad that we were both able to reach the same conclusion about the fix!

Fixed from version http: "0.12.0+4"

not working MultipartRequest("PUT", uri) ,

Was this page helpful?
0 / 5 - 0 ratings