You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 6329315
Browse filesBrowse the repository at this point in the historyBrowse files
A powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, Request Cancellation, File downloading, Timeout etc.
10
10
11
11
### Add dependency
12
12
13
13
```yaml
14
14
dependencies:
15
-
dio: 2.1.x #latest version
15
+
dio: 2.2.x #latest version
16
16
```
17
-
If you are using 1.x , this doc can help you upgrade to 2.x. [Change log](https://github.com/flutterchina/dio/blob/master/CHANGELOG.md)
17
+
> In order to support Flutter Web, v2.2.0 was heavily refactored, so it was not compatible with version 2.1.x. See [here](https://github.com/flutterchina/dio/blob/master/dio/CHANGELOG.md) for a detailed list of updates.
…you can find all examples code [here](https://github.com/flutterchina/dio/tree/master/example).
172
172
173
+
174
+
173
175
## Dio APIs
174
176
175
177
### Creating an instance and set default configs.
@@ -257,11 +259,11 @@ The Options class describes the http request information and configuration. Each
257
259
/// it will be combined and then resolved with the baseUrl.
258
260
String path="";
259
261
260
-
/// The request Content-Type. The default value is [ContentType.JSON].
262
+
/// The request Content-Type. The default value is "application/json; charset=utf-8".
261
263
/// If you want to encode request body with "application/x-www-form-urlencoded",
262
-
/// you can set `ContentType.parse("application/x-www-form-urlencoded")`, and [Dio]
264
+
/// you can set [Headers.formUrlEncodedContentType], and [Dio]
263
265
/// will automatically encode the request body.
264
-
ContentType contentType;
266
+
String contentType;
265
267
266
268
/// [responseType] indicates the type of data that the server will respond with
267
269
/// options which defined in [ResponseType] are `JSON`, `STREAM`, `PLAIN`.
@@ -282,9 +284,10 @@ The Options class describes the http request information and configuration. Each
282
284
283
285
/// Custom field that you can retrieve it later in [Interceptor]、[Transformer] and the [Response] object.
You can lock/unlock the interceptors by calling their `lock()`/`unlock` method. Once the request/response interceptor is locked, the incoming request/response will be added to a queue before they enter the interceptor, they will not be continued until the interceptor is unlocked.
@@ -421,7 +408,7 @@ Because of security reasons, we need all the requests to set up a csrfToken in t
CookieManager Interceptor can help us manage the request/response cookies automaticly. CookieManager depends on `cookieJar` package :
458
-
459
-
> The dio cookie manage API is based on the withdrawn [cookie_jar](https://github.com/flutterchina/cookie_jar).
460
-
461
-
You can create a `CookieJar` or `PersistCookieJar` to manage cookies automatically, and dio use the `CookieJar` by default, which saves the cookies **in RAM**. If you want to persists cookies, you can use the `PersistCookieJar` class, the example codes as follows:
462
-
463
-
```dart
464
-
var dio = new Dio();
465
-
dio.interceptors.add(CookieManager(CookieJar()))
466
-
```
467
-
468
-
`PersistCookieJar` is a cookie manager which implements the standard cookie policy declared in RFC. `PersistCookieJar` persists the cookies in files, so if the application exit, the cookies always exist unless call `delete` explicitly.
469
-
470
-
> Note: In flutter, the path passed to `PersistCookieJar` must be valid(exists in phones and with write access). you can use [path_provider](https://pub.dartlang.org/packages/path_provider) package to get right path
442
+
### Custom Interceptor
471
443
472
-
More details about [cookie_jar](https://github.com/flutterchina/cookie_jar) see : https://github.com/flutterchina/cookie_jar.
444
+
You can custom interceptor by extending the `Interceptor` class. There is an example that implementing a simple cache policy: [custom cache interceptor](https://github.com/flutterchina/dio/blob/master/example/custom_cache_interceptor.dart).
473
445
474
-
### Custom Interceptor
446
+
##Cookie Manager
475
447
476
-
You can custom interceptor by extending the `Interceptor` class. There is an example that implementing a simple cache policy: custom cache interceptor.
448
+
[dio_cookie_manager](https://github.com/flutterchina/dio/tree/master/plugins/cookie_manager) package is a cookie manager for Dio.
477
449
478
450
## Handling Errors
479
451
@@ -514,9 +486,6 @@ try {
514
486
/// The original error/exception object; It's usually not null when `type`
515
487
/// is DioErrorType.DEFAULT
516
488
dynamic error;
517
-
518
-
/// Error stacktrace info
519
-
StackTrace stackTrace;
520
489
}
521
490
```
522
491
@@ -550,22 +519,21 @@ By default, Dio serializes request data(except String type) to `JSON`. To send d
There is an example [here](https://github.com/flutterchina/dio/blob/6de8289ea71b0b7803654caaa2e9d3d47a588ab7/example/options.dart#L41).
559
-
560
528
## Sending FormData
561
529
562
530
You can also send FormData with Dio, which will send data in the `multipart/form-data`, and it supports uploading files.
563
531
564
532
```dart
565
-
FormData formData = new FormData.from({
533
+
FormData formData = FormData.from({
566
534
"name": "wendux",
567
535
"age": 25,
568
-
"file": new UploadFileInfo(new File("./example/upload.txt"), "upload.txt")
@@ -583,6 +551,7 @@ There is a complete example [here](https://github.com/flutterchina/dio/blob/mast
583
551
If you use dio in flutter development, you'd better to decode json in background with [compute] function.
584
552
585
553
```dart
554
+
586
555
// Must be top-level function
587
556
_parseAndDecode(String response) {
588
557
return jsonDecode(response);
@@ -625,6 +594,9 @@ dio.httpClientAdapter = new DefaultHttpClientAdapter();
625
594
`DefaultHttpClientAdapter` provide a callback to set proxy to `dart:io:HttpClient`, for example:
626
595
627
596
```dart
597
+
import 'package:dio/dio.dart';
598
+
import 'package:dio/adapter.dart';
599
+
...
628
600
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
629
601
// config the http client
630
602
client.findProxy = (uri) {
@@ -668,12 +640,16 @@ Another way is creating a `SecurityContext` when create the `HttpClient`:
668
640
669
641
In this way, the format of certificate must be PEM or PKCS12.
670
642
643
+
## Http2 support
644
+
645
+
[dio_http2_adapter](https://github.com/flutterchina/dio/tree/master/plugins/http2_adapter) package is a Dio HttpClientAdapter which support Http/2.0 .
646
+
671
647
## Cancellation
672
648
673
649
You can cancel a request using a *cancel token*. One token can be shared with multiple requests. When a token's `cancel` method invoked, all requests with this token will be cancelled.
Copy file name to clipboardExpand all lines: dio/CHANGELOG.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,33 @@
1
+
# 2.2.0
2
+
3
+
### New features
4
+
5
+
- Support Flutter Web.
6
+
- Extract [CookieManager](https://github.com/flutterchina/dio/tree/master/plugins/cookie_manager) into a separate package(No need for Flutter Web).
7
+
- Provides a [HTTP/2.0 HttpClientAdapter](https://github.com/flutterchina/dio/tree/master/plugins/http2_adapter).
8
+
9
+
### Change List
10
+
11
+
-~~Options.cookies~~
12
+
13
+
-~~Options.connectionTimeout~~ ;We should config connection timed out in `BaseOptions`. For keep-alive reasons, not every request requires a separate connection。
14
+
15
+
-`Options.followRedirects`、`Options.maxRedirects`、`Response.redirects` don't make sense in Flutter Web,because redirection can be automatically handled by browsers.
- The return type of Interceptor's callback changes from `FutureOr<dynamic>` to `Future`. The reason is [here](https://dart.dev/guides/language/effective-dart/design#avoid-using-futureort-as-a-return-type) .
24
+
25
+
- The type of `Response.headers` changes from `HttpHeaders` to `Headers`, because `HttpHeaders` is in "dart:io" library which is not supported in Flutter Web.
0 commit comments