Skip to content

Commit 1f09fd9

Browse files
committed
Preventing "charset" from being added
Preventing charset from being added to the "content-type" header. Based on a workaround for a know http bug: dart-lang/http#184 (comment).
1 parent 2898af8 commit 1f09fd9

File tree

4 files changed

+165
-32
lines changed

4 files changed

+165
-32
lines changed

example/pubspec.lock

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.6.1"
10+
version: "2.8.2"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -21,14 +21,14 @@ packages:
2121
name: characters
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "1.1.0"
24+
version: "1.2.0"
2525
charcode:
2626
dependency: transitive
2727
description:
2828
name: charcode
2929
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "1.2.0"
31+
version: "1.3.1"
3232
clock:
3333
dependency: transitive
3434
description:
@@ -42,14 +42,14 @@ packages:
4242
name: collection
4343
url: "https://pub.dartlang.org"
4444
source: hosted
45-
version: "1.15.0"
45+
version: "1.16.0"
4646
fake_async:
4747
dependency: transitive
4848
description:
4949
name: fake_async
5050
url: "https://pub.dartlang.org"
5151
source: hosted
52-
version: "1.2.0"
52+
version: "1.3.0"
5353
ffi:
5454
dependency: transitive
5555
description:
@@ -106,28 +106,35 @@ packages:
106106
name: js
107107
url: "https://pub.dartlang.org"
108108
source: hosted
109-
version: "0.6.3"
109+
version: "0.6.4"
110110
matcher:
111111
dependency: transitive
112112
description:
113113
name: matcher
114114
url: "https://pub.dartlang.org"
115115
source: hosted
116-
version: "0.12.10"
116+
version: "0.12.11"
117+
material_color_utilities:
118+
dependency: transitive
119+
description:
120+
name: material_color_utilities
121+
url: "https://pub.dartlang.org"
122+
source: hosted
123+
version: "0.1.4"
117124
meta:
118125
dependency: transitive
119126
description:
120127
name: meta
121128
url: "https://pub.dartlang.org"
122129
source: hosted
123-
version: "1.3.0"
130+
version: "1.7.0"
124131
path:
125132
dependency: transitive
126133
description:
127134
name: path
128135
url: "https://pub.dartlang.org"
129136
source: hosted
130-
version: "1.8.0"
137+
version: "1.8.1"
131138
path_provider_linux:
132139
dependency: transitive
133140
description:
@@ -230,7 +237,7 @@ packages:
230237
name: source_span
231238
url: "https://pub.dartlang.org"
232239
source: hosted
233-
version: "1.8.1"
240+
version: "1.8.2"
234241
stack_trace:
235242
dependency: transitive
236243
description:
@@ -265,7 +272,7 @@ packages:
265272
name: test_api
266273
url: "https://pub.dartlang.org"
267274
source: hosted
268-
version: "0.3.0"
275+
version: "0.4.9"
269276
typed_data:
270277
dependency: transitive
271278
description:
@@ -279,7 +286,7 @@ packages:
279286
name: vector_math
280287
url: "https://pub.dartlang.org"
281288
source: hosted
282-
version: "2.1.0"
289+
version: "2.1.2"
283290
win32:
284291
dependency: transitive
285292
description:
@@ -295,5 +302,5 @@ packages:
295302
source: hosted
296303
version: "0.2.0"
297304
sdks:
298-
dart: ">=2.13.0 <3.0.0"
305+
dart: ">=2.17.0-0 <3.0.0"
299306
flutter: ">=2.0.0"

lib/http/intercepted_client.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import 'interceptor_contract.dart';
3434
///the connection alive with the server.
3535
///
3636
///Note: `send` method is not currently supported.
37+
///
38+
enum BodyType { string, list, map }
39+
3740
class InterceptedClient extends BaseClient {
3841
List<InterceptorContract> interceptors;
3942
Duration? requestTimeout;
@@ -197,22 +200,27 @@ class InterceptedClient extends BaseClient {
197200
}) async {
198201
url = url.addParameters(params);
199202

203+
late BodyType bodyType;
204+
200205
Request request = new Request(methodToString(method), url);
201206
if (headers != null) request.headers.addAll(headers);
202207
if (encoding != null) request.encoding = encoding;
203208
if (body != null) {
204209
if (body is String) {
210+
bodyType = BodyType.string;
205211
request.body = body;
206212
} else if (body is List) {
213+
bodyType = BodyType.list;
207214
request.bodyBytes = body.cast<int>();
208215
} else if (body is Map) {
216+
bodyType = BodyType.map;
209217
request.bodyFields = body.cast<String, String>();
210218
} else {
211219
throw new ArgumentError('Invalid request body "$body".');
212220
}
213221
}
214222

215-
var response = await _attemptRequest(request);
223+
var response = await _attemptRequest(request, bodyType);
216224

217225
// Intercept response
218226
response = await _interceptResponse(response);
@@ -231,11 +239,11 @@ class InterceptedClient extends BaseClient {
231239

232240
/// Attempts to perform the request and intercept the data
233241
/// of the response
234-
Future<Response> _attemptRequest(Request request) async {
242+
Future<Response> _attemptRequest(Request request, BodyType bodyType) async {
235243
var response;
236244
try {
237245
// Intercept request
238-
final interceptedRequest = await _interceptRequest(request);
246+
final interceptedRequest = await _interceptRequest(request, bodyType);
239247

240248
var stream = requestTimeout == null
241249
? await send(interceptedRequest)
@@ -247,14 +255,14 @@ class InterceptedClient extends BaseClient {
247255
await retryPolicy!.shouldAttemptRetryOnResponse(
248256
ResponseData.fromHttpResponse(response))) {
249257
_retryCount += 1;
250-
return _attemptRequest(request);
258+
return _attemptRequest(request, bodyType);
251259
}
252260
} on Exception catch (error) {
253261
if (retryPolicy != null &&
254262
retryPolicy!.maxRetryAttempts > _retryCount &&
255263
retryPolicy!.shouldAttemptRetryOnException(error)) {
256264
_retryCount += 1;
257-
return _attemptRequest(request);
265+
return _attemptRequest(request, bodyType);
258266
} else {
259267
rethrow;
260268
}
@@ -265,10 +273,10 @@ class InterceptedClient extends BaseClient {
265273
}
266274

267275
/// This internal function intercepts the request.
268-
Future<Request> _interceptRequest(Request request) async {
276+
Future<Request> _interceptRequest(Request request, BodyType bodyType) async {
269277
for (InterceptorContract interceptor in interceptors) {
270278
RequestData interceptedData = await interceptor.interceptRequest(
271-
data: RequestData.fromHttpRequest(request),
279+
data: RequestData.fromHttpRequest(request, bodyType),
272280
);
273281
request = interceptedData.toHttpRequest();
274282
}

lib/models/request_data.dart

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'dart:typed_data';
23
import 'package:http/http.dart';
34

45
import 'package:http_interceptor/extensions/extensions.dart';
@@ -26,6 +27,10 @@ class RequestData {
2627

2728
dynamic body;
2829

30+
Uint8List? bodyBytes;
31+
32+
Map<String, String>? bodyFields;
33+
2934
/// The encoding used for the request.
3035
Encoding? encoding;
3136

@@ -34,8 +39,10 @@ class RequestData {
3439
required this.baseUrl,
3540
Map<String, String>? headers,
3641
Map<String, dynamic>? params,
37-
this.body,
3842
this.encoding,
43+
this.body,
44+
this.bodyBytes,
45+
this.bodyFields,
3946
}) : headers = headers ?? {},
4047
params = params ?? {};
4148

@@ -47,22 +54,36 @@ class RequestData {
4754
///
4855
/// For now it only supports [Request].
4956
/// TODO(codingalecr): Support for [MultipartRequest] and [StreamedRequest].
50-
factory RequestData.fromHttpRequest(BaseRequest request) {
57+
factory RequestData.fromHttpRequest(BaseRequest request,
58+
[BodyType bodyType = BodyType.string]) {
5159
var params = Map<String, dynamic>();
5260
request.url.queryParametersAll.forEach((key, value) {
5361
params[key] = value;
5462
});
5563
String baseUrl = request.url.origin + request.url.path;
5664

5765
if (request is Request) {
58-
return RequestData(
66+
final requestData = RequestData(
5967
method: methodFromString(request.method),
6068
baseUrl: baseUrl,
6169
headers: request.headers,
62-
body: request.body,
6370
encoding: request.encoding,
6471
params: params,
6572
);
73+
74+
switch (bodyType) {
75+
case BodyType.string:
76+
requestData.body = request.body;
77+
break;
78+
case BodyType.list:
79+
requestData.bodyBytes = request.bodyBytes;
80+
break;
81+
case BodyType.map:
82+
requestData.bodyFields = request.bodyFields;
83+
break;
84+
}
85+
86+
return requestData;
6687
}
6788

6889
throw UnsupportedError(
@@ -76,20 +97,21 @@ class RequestData {
7697

7798
Request request = new Request(methodToString(method), reqUrl.toUri());
7899

79-
request.headers.addAll(headers);
80100
if (encoding != null) request.encoding = encoding!;
81101
if (body != null) {
82102
if (body is String) {
83103
request.body = body as String;
84104
} else if (body is List) {
85-
request.bodyBytes = body?.cast<int>();
105+
request.bodyBytes = bodyBytes!;
86106
} else if (body is Map) {
87-
request.bodyFields = body.cast<String, String>();
107+
request.bodyFields = bodyFields!;
88108
} else {
89109
throw new ArgumentError('Invalid request body "$body".');
90110
}
91111
}
92112

113+
request.headers.addAll(headers);
114+
93115
return request;
94116
}
95117

0 commit comments

Comments
 (0)