Skip to content

Commit ba9a867

Browse files
committed
add options.responseDecoder
1 parent a2c8517 commit ba9a867

File tree

7 files changed

+77
-17
lines changed

7 files changed

+77
-17
lines changed

.travis.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ dart:
77
before_install:
88
- export DISPLAY=:99.0
99
- sh -e /etc/init.d/xvfb start
10-
1110
script:
1211
- pwd
13-
- sh test.sh
14-
after_success:
15-
- bash <(curl -s https://codecov.io/bash)
12+
- sh test.sh

example/cookie_mgr.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import 'package:dio/dio.dart';
33

44
main() async {
55
var dio = new Dio();
6+
var cookieJar=CookieJar();
67
dio.interceptors
7-
..add(CookieManager(CookieJar()))
8+
..add(CookieManager(cookieJar))
89
..add(LogInterceptor(responseBody: false));
910
await dio.get("https://baidu.com/");
11+
// Print cookies
12+
print(cookieJar.loadForRequest(Uri.parse("https://baidu.com/")));
1013
// second request with the cookie
1114
await dio.get("https://baidu.com/");
1215
}

example/test.dart

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'dart:io';
2+
import 'package:dio/dio.dart';
3+
4+
main() async {
5+
var dio = Dio();
6+
7+
dio.interceptors.add(InterceptorsWrapper(onError: (e){
8+
print("xxxx");
9+
print(e);
10+
}));
11+
12+
void _testDio() async {
13+
CancelToken token = CancelToken();
14+
var url = "https://github.com/flutterchina12121/dio";
15+
16+
Future.delayed(const Duration(milliseconds: 0), () async {
17+
try {
18+
Response response = await dio.get(url, cancelToken: token);
19+
20+
print("response");
21+
if (response.statusCode == 200) {
22+
print("request success");
23+
} else {
24+
print("request failed");
25+
}
26+
} catch (e) {
27+
28+
print(e);
29+
}
30+
});
31+
32+
Future.delayed(const Duration(milliseconds: 250), () {
33+
print("manual cancel");
34+
if (!token.isCancelled) {
35+
token.cancel();
36+
}
37+
});
38+
}
39+
40+
await _testDio();
41+
}

package_src/lib/src/dio.dart

+4-8
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,6 @@ class Dio {
690690
requestOptions.responseType = ResponseType.json;
691691
}
692692
}
693-
if (data is FormData) {
694-
requestOptions.headers[HttpHeaders.contentTypeHeader] =
695-
'multipart/form-data; boundary=${data.boundary.substring(2)}';
696-
}
697693
Future<Response<T>> future =
698694
_checkIfNeedEnqueue<T>(interceptors.requestLock, () {
699695
Future ret = _executeInterceptors<RequestOptions>(
@@ -841,10 +837,10 @@ class Dio {
841837
return false;
842838
});
843839
} else if (data is FormData) {
844-
assert(
845-
options.headers[HttpHeaders.contentTypeHeader] ==
846-
'multipart/form-data; boundary=${data.boundary.substring(2)}',
847-
"You shouldn't change the value of content-type in request headers when sending FormData.");
840+
if (data is FormData) {
841+
options.headers[HttpHeaders.contentTypeHeader] =
842+
'multipart/form-data; boundary=${data.boundary.substring(2)}';
843+
}
848844
stream = data.stream;
849845
length = data.length;
850846
} else {

package_src/lib/src/options.dart

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:io';
22
import 'dio.dart';
33
import 'cancel_token.dart';
44
import 'transformer.dart';
5+
import 'adapter.dart';
56

67
/// ResponseType indicates which transformation should
78
/// be automatically applied to the response data by Dio.
@@ -28,6 +29,7 @@ enum ResponseType {
2829
}
2930

3031
typedef ValidateStatus = bool Function(int status);
32+
typedef ResponseDecoder = String Function(List<int> responseBytes, RequestOptions options, ResponseBody responseBody);
3133

3234
/// The common config for the Dio instance.
3335
/// `dio.options` is a instance of [BaseOptions]
@@ -47,6 +49,7 @@ class BaseOptions extends _RequestConfig {
4749
bool receiveDataWhenStatusError = true,
4850
bool followRedirects = true,
4951
int maxRedirects = 5,
52+
ResponseDecoder responseDecoder,
5053
}) : super(
5154
method: method,
5255
connectTimeout: connectTimeout,
@@ -60,6 +63,7 @@ class BaseOptions extends _RequestConfig {
6063
followRedirects: followRedirects,
6164
cookies: cookies,
6265
maxRedirects: maxRedirects,
66+
responseDecoder: responseDecoder,
6367
);
6468

6569
/// Create a new Option from current instance with merging attributes.
@@ -79,6 +83,7 @@ class BaseOptions extends _RequestConfig {
7983
bool receiveDataWhenStatusError,
8084
bool followRedirects,
8185
int maxRedirects,
86+
ResponseDecoder responseDecoder,
8287
}) {
8388
return new BaseOptions(
8489
method: method ?? this.method,
@@ -95,6 +100,7 @@ class BaseOptions extends _RequestConfig {
95100
receiveDataWhenStatusError ?? this.receiveDataWhenStatusError,
96101
followRedirects: followRedirects ?? this.followRedirects,
97102
maxRedirects: maxRedirects ?? this.maxRedirects,
103+
responseDecoder: responseDecoder ?? this.responseDecoder,
98104
);
99105
}
100106

@@ -121,6 +127,7 @@ class Options extends _RequestConfig {
121127
bool receiveDataWhenStatusError,
122128
bool followRedirects,
123129
int maxRedirects,
130+
ResponseDecoder responseDecoder,
124131
}) : super(
125132
method: method,
126133
connectTimeout: connectTimeout,
@@ -135,6 +142,7 @@ class Options extends _RequestConfig {
135142
followRedirects: followRedirects,
136143
cookies: cookies,
137144
maxRedirects: maxRedirects,
145+
responseDecoder: responseDecoder,
138146
);
139147

140148
/// Create a new Option from current instance with merging attributes.
@@ -152,6 +160,7 @@ class Options extends _RequestConfig {
152160
bool receiveDataWhenStatusError,
153161
bool followRedirects,
154162
int maxRedirects,
163+
ResponseDecoder responseDecoder,
155164
}) {
156165
return new Options(
157166
method: method ?? this.method,
@@ -167,6 +176,7 @@ class Options extends _RequestConfig {
167176
receiveDataWhenStatusError ?? this.receiveDataWhenStatusError,
168177
followRedirects: followRedirects ?? this.followRedirects,
169178
maxRedirects: maxRedirects ?? this.maxRedirects,
179+
responseDecoder: responseDecoder ?? this.responseDecoder,
170180
);
171181
}
172182
}
@@ -192,6 +202,7 @@ class RequestOptions extends Options {
192202
bool receiveDataWhenStatusError = true,
193203
bool followRedirects = true,
194204
int maxRedirects,
205+
ResponseDecoder responseDecoder,
195206
}) : super(
196207
method: method,
197208
connectTimeout: connectTimeout,
@@ -206,6 +217,7 @@ class RequestOptions extends Options {
206217
receiveDataWhenStatusError: receiveDataWhenStatusError,
207218
followRedirects: followRedirects,
208219
maxRedirects: maxRedirects,
220+
responseDecoder: responseDecoder,
209221
);
210222

211223
/// generate uri
@@ -260,6 +272,7 @@ class _RequestConfig {
260272
this.receiveDataWhenStatusError = true,
261273
this.followRedirects = true,
262274
this.maxRedirects = 5,
275+
this.responseDecoder,
263276
}) : this.headers = headers ?? {},
264277
this.extra = extra ?? {};
265278

@@ -328,4 +341,8 @@ class _RequestConfig {
328341

329342
/// Custom Cookies for every request
330343
List<Cookie> cookies;
344+
345+
/// The default response decoder is utf8decoder, you can set custom
346+
/// decoder by this option, it will be used in [Transformer].
347+
ResponseDecoder responseDecoder;
331348
}

package_src/lib/src/transformer.dart

+6-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ class DefaultTransformer extends Transformer {
135135
await completer.future;
136136
}
137137
if (options.responseType == ResponseType.bytes) return buffer;
138-
String responseBody = utf8.decode(buffer, allowMalformed: true);
138+
String responseBody;
139+
if (options.responseDecoder != null) {
140+
responseBody = options.responseDecoder(buffer, options, response..stream=null);
141+
} else {
142+
responseBody = utf8.decode(buffer, allowMalformed: true);
143+
}
139144
if (responseBody != null &&
140145
responseBody.isNotEmpty &&
141146
options.responseType == ResponseType.json &&

package_src/test/dio_test.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ void main() {
140140
"file",
141141
UploadFileInfo(File("./pubspec.yaml"), "pubspec.yaml"),
142142
);
143-
await dio.post("/test", data: formData);
144-
formData.clear();
145-
expect(formData.length, 0);
143+
print(formData);
144+
//await dio.post("/test", data: formData);
145+
//formData.clear();
146+
//expect(formData.length, 0);
146147
});
147148
});
148149

0 commit comments

Comments
 (0)