diff --git a/pkgs/cupertino_http/lib/src/cupertino_client.dart b/pkgs/cupertino_http/lib/src/cupertino_client.dart index c36e26975f..f535ae3c8f 100644 --- a/pkgs/cupertino_http/lib/src/cupertino_client.dart +++ b/pkgs/cupertino_http/lib/src/cupertino_client.dart @@ -307,6 +307,13 @@ class CupertinoClient extends BaseClient { ..headersCommaValues = request.headers ..maxRedirects = request.maxRedirects; + if (profile != null && request.contentLength != null) { + profile.requestData.headersListValues = { + 'Content-Length': ['${request.contentLength}'], + ...profile.requestData.headers! + }; + } + final urlRequest = MutableURLRequest.fromUrl(request.url) ..httpMethod = request.method; @@ -315,12 +322,6 @@ class CupertinoClient extends BaseClient { // `httpBodyStream` requires a lot of expensive setup and data passing. urlRequest.httpBody = Data.fromList(request.bodyBytes); profile?.requestData.bodySink.add(request.bodyBytes); - if (profile != null) { - final headers = - Map>.from(profile.requestData.headers!); - headers['Content-Length'] = ['${request.contentLength}']; - profile.requestData.headersListValues = headers; - } } else if (await _hasData(stream) case (true, final s)) { // If the request is supposed to be bodyless (e.g. GET requests) // then setting `httpBodyStream` will cause the request to fail - @@ -331,12 +332,6 @@ class CupertinoClient extends BaseClient { final splitter = StreamSplitter(s); urlRequest.httpBodyStream = splitter.split(); unawaited(profile.requestData.bodySink.addStream(splitter.split())); - if (request.contentLength != null) { - final headers = - Map>.from(profile.requestData.headers!); - headers['Content-Length'] = ['${request.contentLength}']; - profile.requestData.headersListValues = headers; - } } }