Viewing version 0.0.1. Open the latest package page.
dio
0.0.1A powerful Http client for Dart, which supports Interceptors, Global configuration, FormData and soon on.
Readme
dio
A powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, File downloading etc. and Dio is very easy to use.
Add dependency
dependencies:
dio: ^0.0.1
Super simple to use
import 'package:dio/dio.dart';
Dio dio = new Dio();
Response<String> response=await dio.get("https://www.google.com/");
print(response.data);
Table of contents
Examples Dio APIs Request Options Response Schema Interceptors Handling Errors Using application/x-www-form-urlencoded format Sending FormData Transformer Set proxy and HttpClient config Features and bugs
Examples
Performing a GET request:
Response response;
response=await dio.get("/test?id=12&name=wendu")
print(response.data.toString());
// Optionally the request above could also be done as
response=await dio.get("/test",data:{"id":12,"name":"wendu"})
print(response.data.toString());
Performing a POST request:
response=await dio.post("/test",data:{"id":12,"name":"wendu"})
Performing multiple concurrent requests:
response= await Future.wait([dio.post("/info"),dio.get("/token")]);
Downloading a file:
response=await dio.download("https://www.google.com/","./xx.html")
Sending FormData:
FormData formData = new FormData.from({
"name": "wendux",
"age": 25,
});
response = await dio.post("/info", data: formData)
Uploading multiple files to server by FormData:
FormData formData = new FormData.from({
"name": "wendux",
"age": 25,
"file1": new UploadFileInfo(new File("./upload.txt"), "upload1.txt")
"file2": new UploadFileInfo(new File("./upload.txt"), "upload2.txt")
});
response = await dio.post("/info", data: formData)
Dio APIs
Creating an instance
You can create instance of Dio with a optional Options object:
Dio dio = new Dio; // with default Options
//or
Options options= new Options(baseUrl:"https://www.xx.com/api" );
Dio dio = new Dio(options);
The core API in Dio instance is :
Future request(String path, {data, Options options})
response=await request("/test", data: {"id":12,"name":"xx"}, new Options(method:"GET"));
Request method aliases
For convenience aliases have been provided for all supported request methods.
Future get(path, {data, Options options})
Future post(path, {data, Options options})
Future put(path, {data, Options options})
Future delete(path, {data, Options options})
Future head(path, {data, Options options})
Future put(path, {data, Options options})
Future path(path, {data, Options options})
Request Options
These are the available config options for making requests. Requests will default to GET if method is not specified.
{
/// Http method.
String method;
/// Request base url, it can contain sub path, like: "https://www.google.com/api/".
String baseUrl;
/// Http request headers.
Map<String, dynamic> headers;
/// Reserved, not support yet!
int timeout;
/// Request data, can be any type.
T data;
/// If the `path` starts with "http(s)", the `baseURL` will be ignored, otherwise,
/// it will be combined and then resolved with the baseUrl.
String path="";
/// The request Content-Type. The default value is [ContentType.JSON].
/// If you want to encode request body with "application/x-www-form-urlencoded",
/// you can set `ContentType.parse("application/x-www-form-urlencoded")`, and [Dio]
/// will automatically encode the request body.
ContentType contentType;
/// [responseType] indicates the type of data that the server will respond with
/// options which defined in [ResponseType] are `JSON`, `STREAM`, `PLAIN`.
///
/// The default value is `JSON`, dio will parse response string to json object automatically
/// when the content-type of response is "application/json".
///
/// If you want to receive response data with binary bytes, for example,
/// downloading a image, use `STREAM`.
///
/// If you want to receive the response data with String, use `PLAIN`.
ResponseType responseType;
/// Custom field that you can retrieve it later in [Interceptor]、[TransFormer] and the [Response] object.
Map<String, dynamic> extra;
}
Response Schema
The response for a request contains the following information.
{
/// Response body. may have been transformed, please refer to [ResponseType].
T data;
/// Response headers.
HttpHeaders headers;
/// The corresponding request info.
Options request;
/// Http status code.
int statusCode;
}
When request is succeed, you will receive the response as follows:
Response response=await dio.get("https://www.google.com");
print(response.data);
print(response.headers);
print(response.request);
print(statusCode);
Interceptors
Each Dio instance has a RequestInterceptor and a ResponseInterceptor, by which you can intercept requests or responses before they are handled by then or catchError.
dio.interceptor.request.onSend = (Options options){
// Do something before request is sent
return options; //continue
// If you want to resolve the request with some custom data,
// you can return a `Response` object or return `dio.resolve(data)`.
// If you want to reject the request with a error message,
// you can return a `DioError` object or return `dio.reject(errMsg)`
}
dio.interceptor.response.onSuccess = (Response response) {
// Do something with response data
return response; // continue
};
dio.interceptor.response.onError = (DioError e){
// Do something with response error
return DioError;//continue
}
If you may need to remove an interceptor later you can.
dio.interceptor.request.onSend=null;
dio.interceptor.response.onSuccess=null;
dio.interceptor.response.onError=null;
Resolve and reject the request
In all interceptors, you can interfere with thire execution flow. If you want to resolve the request/response with some custom data, you can return a Response object or return dio.resolve(data). If you want to reject the request/response with a error message, you can return a DioError object or return dio.reject(errMsg) .
dio.interceptor.request.onSend = (Options options){
return dio.resolve("fake data")
}
Response response= await dio.get("/test");
print(response.data);//"fake data"
Supports Async tasks in Interceptors
Interceptors not only support synchronous tasks , but also supports asynchronous tasks, for example:
dio.interceptor.request.onSend = (Options options) async{
//...If no token, request token firstly.
Response response = await dio.get("/token");
//Set the token to headers
options.headers["token"] = response.data["data"]["token"];
return options; //continue
}
Lock/unlock the interceptors
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.
tokenDio=new Dio(); //Create a new instance to request the token.
tokenDio.options=dio;
dio.interceptor.request.onSend = (Options options) async{
// If no token, request token firstly and lock this interceptor
// to prevent other request enter this interceptor.
dio.interceptor.request.lock();
// We use a new Dio(to avoid dead lock) instance to request token.
Response response = await tokenDio.get("/token");
//Set the token to headers
options.headers["token"] = response.data["data"]["token"];
dio.interceptor.request.unlock()
return options; //continue
}
Handling Errors
try {
//404
await dio.get("https://wendux.github.io/xsddddd");
} on DioError catch(e) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx and is also not 304.
if(e.response) {
print(e.response.data)
print(e.response.headers)
print(e.response.request)
} else{
// Something happened in setting up or sending the request that triggered an Error
print(e.request)
print(e.message)
}
}
Using application/x-www-form-urlencoded format
By default, Dio serializes request data(except String type) to JSON. To send data in the application/x-www-form-urlencoded format instead, you can :
//Instance level
dio.options.contentType=ContentType.parse("application/x-www-form-urlencoded");
//or works once
dio.post("/info",data:{"id":5}, options: new Options(contentType:ContentType.parse("application/x-www-form-urlencoded")))
Sending FormData
You can also send FormData with Dio, which will send data in the multipart/form-data, and it supports upload files.
FormData formData = new FormData.from({
"name": "wendux",
"age": 25,
"file": new UploadFileInfo(new File("./example/upload.txt"), "upload.txt")
});
response = await dio.post("/info", data: formData)
Note: Just the post method suppots FormData.
Transformer
TransFormer allows changes to the request/response data before it is sent/received to/from the server. This is only applicable for request methods 'PUT', 'POST', and 'PATCH'. Dio has already implemented a DefaultTransformer, and as the default TransFormer. If you want to custom the transformation of request/response data, you can provide a TransFormer by your self, and replace the DefaultTransformer by setting the dio.transformer.
Set proxy and HttpClient config
Dio use HttpClient to send http request, so you can config the dio.httpClient to support porxy, for example:
dio.httpClient.findProxy = (uri) {
//proxy all request to localhost:8888
return "PROXY localhost:8888";
};
Features and bugs
Please file feature requests and bugs at the issue tracker.
Changelog
0.0.1
- Initial version, created by Stagehand
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'dio:0.0.1'pubspec.yaml
name: dio
description: A powerful Http client for Dart, which supports Interceptors, Global configuration, FormData and soon on.
version: 0.0.1
homepage: https://github.com/flutterchina/dio
author: wendux <824783146@qq.com>
environment:
sdk: '>=1.20.1 <2.0.0'
flutter: "^0.1.2"
#dependencies:
#path: ^1.4.1
dev_dependencies:
test: ^0.12.0
documentation:
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | Cached · Upstream SHA-256 matched | Download | 5bc41fc8…eddd8b |
| 0.0.2 | Cached · Upstream SHA-256 matched | Download | 4cac0107…91fd69 |
| 0.0.3 | Cached · Upstream SHA-256 matched | Download | 7ce1180b…b9fad8 |
| 0.0.4 | Cached · Upstream SHA-256 matched | Download | a12b23ee…e96eb7 |
| 0.0.5 | Cached · Upstream SHA-256 matched | Download | 169d5b7f…062689 |
| 0.0.6 | Cached · Upstream SHA-256 matched | Download | 9e74de0d…dafe09 |
| 0.0.7 | Cached · Upstream SHA-256 matched | Download | 22e05669…d96dfa |
| 0.0.8 | Cached · Upstream SHA-256 matched | Download | 50abafcc…ae6d76 |
| 0.0.9 | Cached · Upstream SHA-256 matched | Download | 5f6f9d5a…ef2fc2 |
| 0.0.10 | Cached · Upstream SHA-256 matched | Download | e59ebb5b…17b838 |
| 0.0.11 | Cached · Upstream SHA-256 matched | Download | cbeab948…689e68 |
| 0.0.12 | Cached · Upstream SHA-256 matched | Download | b485e914…de23cc |
| 0.0.13 | Cached · Upstream SHA-256 matched | Download | 123c5841…4317d6 |
| 0.0.14 | Cached · Upstream SHA-256 matched | Download | 72cb0bbc…e21b15 |
| 0.1.1 | Cached · Upstream SHA-256 matched | Download | 62ab575e…a97e58 |
| 0.1.2 | Cached · Upstream SHA-256 matched | Download | bda27230…2a7105 |
| 0.1.3 | Cached · Upstream SHA-256 matched | Download | ae23824f…2022be |
| 0.1.4 | Cached · Upstream SHA-256 matched | Download | da995108…68e860 |
| 0.1.5 | Cached · Upstream SHA-256 matched | Download | 82cc6e7e…44be8d |
| 0.1.6 | Cached · Upstream SHA-256 matched | Download | 0afe7b54…ca098b |
| 0.1.7 | Cached · Upstream SHA-256 matched | Download | 8ffea790…949274 |
| 0.1.8 | Cached · Upstream SHA-256 matched | Download | 39f62ea9…f765a0 |
| 1.0.0 | Cached · Upstream SHA-256 matched | Download | 4a1f41f7…3cdf27 |
| 1.0.1 | Cached · Upstream SHA-256 matched | Download | 7cc5af51…87cc00 |
| 1.0.2 | Cached · Upstream SHA-256 matched | Download | 7fecdca6…3db3e3 |
| 1.0.3 | Cached · Upstream SHA-256 matched | Download | 0fddaf68…f1a2e3 |
| 1.0.4 | Cached · Upstream SHA-256 matched | Download | ec5b8a46…27563c |
| 1.0.5 | Cached · Upstream SHA-256 matched | Download | 2a8b2d04…e73a92 |
| 1.0.6 | Cached · Upstream SHA-256 matched | Download | 3853004e…ce3644 |
| 1.0.7 | Cached · Upstream SHA-256 matched | Download | c44cfae4…2f77aa |
| 1.0.8 | Cached · Upstream SHA-256 matched | Download | 795030f2…37d5ba |
| 1.0.9 | Cached · Upstream SHA-256 matched | Download | 30684a27…c5300b |
| 1.0.10 | Cached · Upstream SHA-256 matched | Download | 3915eca0…9a138e |
| 1.0.11 | Cached · Upstream SHA-256 matched | Download | 830d5c1e…264e31 |
| 1.0.12 | Cached · Upstream SHA-256 matched | Download | 0f20e63a…6cb856 |
| 1.0.13 | Cached · Upstream SHA-256 matched | Download | f2fb84fd…dca6a8 |
| 1.0.14 | Cached · Upstream SHA-256 matched | Download | a08bfb31…dcfcb4 |
| 1.0.15 | Cached · Upstream SHA-256 matched | Download | 127dead5…56ac22 |
| 1.0.16 | Cached · Upstream SHA-256 matched | Download | 31159699…00b06b |
| 1.0.17 | Cached · Upstream SHA-256 matched | Download | af70e1e1…44568c |
| 2.0.0 | Cached · Upstream SHA-256 matched | Download | f43c90e8…a8efdb |
| 2.0.1 | Cached · Upstream SHA-256 matched | Download | e3f4a06b…f48e30 |
| 2.0.2 | Cached · Upstream SHA-256 matched | Download | b2494d87…14ba57 |
| 2.0.3 | Cached · Upstream SHA-256 matched | Download | e8b10c4a…3b7cf3 |
| 2.0.4 | Cached · Upstream SHA-256 matched | Download | c5573672…1e12b2 |
| 2.0.5 | Cached · Upstream SHA-256 matched | Download | 7a01aad5…61e2e5 |
| 2.0.6 | Cached · Upstream SHA-256 matched | Download | 6ca3e0fa…b8aa07 |
| 2.0.7 | Cached · Upstream SHA-256 matched | Download | 95e034ef…617a60 |
| 2.0.8 | Cached · Upstream SHA-256 matched | Download | 64ce4aef…0fe1f2 |
| 2.0.9 | Cached · Upstream SHA-256 matched | Download | dcf4d22e…b3a294 |
| 2.0.10 | Cached · Upstream SHA-256 matched | Download | e848ec5b…e00d23 |
| 2.0.11 | Cached · Upstream SHA-256 matched | Download | e10bc8ba…fb0869 |
| 2.0.12 | Cached · Upstream SHA-256 matched | Download | b952b98b…e51cac |
| 2.0.13 | Cached · Upstream SHA-256 matched | Download | ad0a5ec2…42703f |
| 2.0.14 | Cached · Upstream SHA-256 matched | Download | 760754bc…d9d185 |
| 2.0.15 | Cached · Upstream SHA-256 matched | Download | 106febea…74f9bb |
| 2.0.16 | Cached · Upstream SHA-256 matched | Download | 78baa870…4febcb |
| 2.0.17 | Cached · Upstream SHA-256 matched | Download | fab3b27d…b34d3f |
| 2.0.18 | Cached · Upstream SHA-256 matched | Download | cf252dc1…66621f |
| 2.0.19 | Cached · Upstream SHA-256 matched | Download | ba1241f6…174412 |
| 2.0.20 | Cached · Upstream SHA-256 matched | Download | 05fd4795…2d75c7 |
| 2.0.21 | Cached · Upstream SHA-256 matched | Download | 6ec20e7a…845103 |
| 2.0.22 | Cached · Upstream SHA-256 matched | Download | 9516eecb…0d28e9 |
| 2.0.23 | Cached · Upstream SHA-256 matched | Download | 4c44720a…2383c1 |
| 2.1.0 | Cached · Upstream SHA-256 matched | Download | 5a162a32…6ebd7b |
| 2.1.1 | Cached · Upstream SHA-256 matched | Download | 0585f94d…a1ce01 |
| 2.1.2 | Cached · Upstream SHA-256 matched | Download | 76692010…2064d0 |
| 2.1.3 | Cached · Upstream SHA-256 matched | Download | 1283cfd5…57805b |
| 2.1.4 | Cached · Upstream SHA-256 matched | Download | 4057b7d2…26c96e |
| 2.1.5 | Cached · Upstream SHA-256 matched | Download | 4cb1357b…1c0b9e |
| 2.1.6 | Cached · Upstream SHA-256 matched | Download | 4593d2c3…3fb868 |
| 2.1.7 | Cached · Upstream SHA-256 matched | Download | 0e127cea…61b481 |
| 2.1.8 | Cached · Upstream SHA-256 matched | Download | 815503e6…1be1d9 |
| 2.1.9 | Cached · Upstream SHA-256 matched | Download | 2566bd41…5f879e |
| 2.1.10 | Cached · Upstream SHA-256 matched | Download | 844d05b5…157743 |
| 2.1.11 | Cached · Upstream SHA-256 matched | Download | 6107ab69…2a9e71 |
| 2.1.12 | Cached · Upstream SHA-256 matched | Download | fcc9326b…a64142 |
| 2.1.13 | Cached · Upstream SHA-256 matched | Download | e242b7e1…7f13f3 |
| 2.1.14 | Cached · Upstream SHA-256 matched | Download | 29fcb893…ed6463 |
| 2.1.15 | Cached · Upstream SHA-256 matched | Download | 3438f224…2d1b37 |
| 2.1.16 | Cached · Upstream SHA-256 matched | Download | 5fb66ffd…a4835d |
| 2.2.0 | Cached · Upstream SHA-256 matched | Download | a7e5346e…4565bf |
| 2.2.1 | Cached · Upstream SHA-256 matched | Download | 479c30bd…ad427c |
| 2.2.2 | Cached · Upstream SHA-256 matched | Download | 8ac4d649…17fcb3 |
| 3.0.0-dev.1 | Cached · Upstream SHA-256 matched | Download | 03ce7b80…4faac2 |
| 3.0.0 | Cached · Upstream SHA-256 matched | Download | f2b8c907…9cfad2 |
| 3.0.1 | Cached · Upstream SHA-256 matched | Download | 7c54b490…9b2208 |
| 3.0.2-dev.1 | Cached · Upstream SHA-256 matched | Download | 35ac5f65…d4ae3f |
| 3.0.2 | Cached · Upstream SHA-256 matched | Download | d0576184…0d7635 |
| 3.0.3 | Cached · Upstream SHA-256 matched | Download | d76e0f80…f219bc |
| 3.0.4 | Cached · Upstream SHA-256 matched | Download | df5b4f12…579ad7 |
| 3.0.5 | Cached · Upstream SHA-256 matched | Download | f77f0947…d69d7e |
| 3.0.6 | Cached · Upstream SHA-256 matched | Download | 9ca0e64d…9de525 |
| 3.0.7 | Cached · Upstream SHA-256 matched | Download | d71f4317…e7d25c |
| 3.0.8-dev.1 | Cached · Upstream SHA-256 matched | Download | 1b028070…e4322b |
| 3.0.8 | Cached · Upstream SHA-256 matched | Download | 155a7f6f…99664b |
| 3.0.9 | Cached · Upstream SHA-256 matched | Download | 49c26d80…876d06 |
| 3.0.10 | Cached · Upstream SHA-256 matched | Download | 11979099…bcdd27 |
| 4.0.0-beta1 | Cached · Upstream SHA-256 matched | Download | e05c44c4…5cb588 |
| 4.0.0-beta2 | Cached · Upstream SHA-256 matched | Download | c650d085…e93e7c |
| 4.0.0-beta3 | Cached · Upstream SHA-256 matched | Download | 186f0697…d4634a |
| 4.0.0-beta4 | Cached · Upstream SHA-256 matched | Download | 25d55004…048065 |
| 4.0.0-beta5 | Cached · Upstream SHA-256 matched | Download | 4f625ee7…2e6c0a |
| 4.0.0-beta6 | Cached · Upstream SHA-256 matched | Download | abcc1d1d…b04235 |
| 4.0.0-beta7 | Cached · Upstream SHA-256 matched | Download | 14c118b5…9d8eb5 |
| 4.0.0-prev1 | Cached · Upstream SHA-256 matched | Download | 1b5e25c8…ff684f |
| 4.0.0-prev2 | Cached · Upstream SHA-256 matched | Download | b4f0f86a…bc773b |
| 4.0.0-prev3 | Cached · Upstream SHA-256 matched | Download | 0ee6bffb…a82e89 |
| 4.0.0 | Cached · Upstream SHA-256 matched | Download | bf173c8b…612947 |
| 4.0.1 | Cached · Upstream SHA-256 matched | Download | 826fe225…44a60f |
| 4.0.2-beta1 | Cached · Upstream SHA-256 matched | Download | ec4c0dd1…821c6b |
| 4.0.2 | Cached · Upstream SHA-256 matched | Download | 5c290dbf…77057f |
| 4.0.3 | Cached · Upstream SHA-256 matched | Download | 06fc445e…bb0a9e |
| 4.0.4 | Cached · Upstream SHA-256 matched | Download | 5fd6e152…fd7b1c |
| 4.0.5-beta1 | Cached · Upstream SHA-256 matched | Download | 605f4b72…3f5aa5 |
| 4.0.5 | Cached · Upstream SHA-256 matched | Download | dad4ffe0…6a996a |
| 4.0.6 | Cached · Upstream SHA-256 matched | Download | 7d328c4d…75e2e8 |
| 5.0.0 | Cached · Upstream SHA-256 matched | Download | 9fdbf71b…67d316 |
| 5.0.1 | Cached · Upstream SHA-256 matched | Download | 3709d746…e6287e |
| 5.0.2 | Cached · Upstream SHA-256 matched | Download | 2644a9e0…070d7b |
| 5.0.3 | Cached · Upstream SHA-256 matched | Download | 3e5c4a94…32100b |
| 5.1.0 | Cached · Upstream SHA-256 matched | Download | f7c26ddb…f8bfce |
| 5.1.1 | Cached · Upstream SHA-256 matched | Download | 0894a098…0f11e9 |
| 5.1.2 | Cached · Upstream SHA-256 matched | Download | 347d56c2…e05059 |
| 5.2.0 | Cached · Upstream SHA-256 matched | Download | e44ce32c…c62e6d |
| 5.2.0+1 | Cached · Upstream SHA-256 matched | Download | b99b1d56…81bdf2 |
| 5.2.1 | Cached · Upstream SHA-256 matched | Download | dfda195c…711d1e |
| 5.2.1+1 | Cached · Upstream SHA-256 matched | Download | a9d76e72…6752b8 |
| 5.3.0 | Cached · Upstream SHA-256 matched | Download | 3866d67f…b49993 |
| 5.3.1 | Cached · Upstream SHA-256 matched | Download | 9d6445da…cbdd2f |
| 5.3.2 | Cached · Upstream SHA-256 matched | Download | ce75a1b4…bc4197 |
| 5.3.3 | Cached · Upstream SHA-256 matched | Download | 417e2a6f…0140b7 |
| 5.3.4 | Cached · Upstream SHA-256 matched | Download | 01870acd…dfea4c |
| 5.4.0 | Cached · Upstream SHA-256 matched | Download | 797e1e34…1645c3 |
| 5.4.1 | Cached · Upstream SHA-256 matched | Download | 49af2838…6ce5a8 |
| 5.4.2 | Cached · Upstream SHA-256 matched | Download | 50fec961…abeca9 |
| 5.4.2+1 | Cached · Upstream SHA-256 matched | Download | 0978e9a3…70217f |
| 5.4.3 | Cached · Upstream SHA-256 matched | Download | 639179e1…834801 |
| 5.4.3+1 | Cached · Upstream SHA-256 matched | Download | 11e40df5…9034b5 |
| 5.5.0 | Cached · Upstream SHA-256 matched | Download | 77befddd…0de0c8 |
| 5.5.0+1 | Cached · Upstream SHA-256 matched | Download | e17f6b30…e73714 |
| 5.6.0 | Cached · Upstream SHA-256 matched | Download | 0dfb6b6a…715fb0 |
| 5.7.0 | Cached · Upstream SHA-256 matched | Download | 5598aa79…efc260 |
| 5.8.0 | Cached · Upstream SHA-256 matched | Download | 5fe8c827…c8d5b0 |
| 5.8.0+1 | Cached · Upstream SHA-256 matched | Download | 253a18bb…c8dbc9 |
| 5.9.0 | Cached · Upstream SHA-256 matched | Download | d90ee579…ac66a9 |
| 5.9.1 | Cached · Upstream SHA-256 matched | Download | b9d46fae…e57b25 |
| 5.9.2 | Cached · Upstream SHA-256 matched | Download | aff32c08…e64f8c |
| 5.10.0 | Cached · Upstream SHA-256 matched | Download | ea2bad3c…0922b0 |
| 5.11.0 | Cached · Upstream SHA-256 matched | Download | 0df44ebb…abbb3c |
| 5.11.1 | Cached · Upstream SHA-256 matched | Download | 852ec3b4…4192c1 |
Dependencies
No direct dependencies declared.