diff --git a/chopper/lib/src/request.dart b/chopper/lib/src/request.dart index 1441ef3d..0f134f7d 100644 --- a/chopper/lib/src/request.dart +++ b/chopper/lib/src/request.dart @@ -138,16 +138,23 @@ base class Request extends http.BaseRequest with EquatableMixin { @visibleForTesting http.Request toHttpRequest() { final http.Request request = http.Request(method, url) - ..followRedirects = followRedirects - ..headers.addAll(headers); + ..followRedirects = followRedirects; - if (body != null) { + if (body == null) { + request.headers.addAll(headers); + } else { if (body is String) { - request.body = body; + request + ..headers.addAll(headers) + ..body = body; } else if (body is List) { - request.bodyBytes = body; + request + ..bodyBytes = body + ..headers.addAll(headers); } else if (body is Map) { - request.bodyFields = body; + request + ..headers.addAll(headers) + ..bodyFields = body; } else { throw ArgumentError.value('$body', 'body'); } diff --git a/chopper/test/base_test.dart b/chopper/test/base_test.dart index dee04012..f1e76dad 100644 --- a/chopper/test/base_test.dart +++ b/chopper/test/base_test.dart @@ -7,6 +7,7 @@ import 'package:chopper/chopper.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:test/test.dart'; +import 'package:transparent_image/transparent_image.dart'; import 'test_service.dart'; import 'test_service_variable.dart'; @@ -763,6 +764,25 @@ void main() { expect(request.bodyBytes, equals([1, 2, 3])); }); + test('BodyBytes does not have charset header', () { + final request = Request( + HttpMethod.Post, + Uri.parse('https://foo/'), + Uri.parse(''), + headers: { + 'authorization': 'Bearer fooBarBaz', + 'x-foo': 'bar', + }, + body: kTransparentImage, + ).toHttpRequest(); + + expect(request.headers['authorization'], equals('Bearer fooBarBaz')); + expect(request.headers['x-foo'], equals('bar')); + expect(request.headers['content-type'], isNull); + expect(request.headers['content-type'], isNot(contains('charset='))); + expect(request.bodyBytes, equals(kTransparentImage)); + }); + test('BodyFields', () { final request = Request( HttpMethod.Post,