dio 2.0.6
dio: ^2.0.6 copied to clipboard

outdated

A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.

example/dio.dart

import 'dart:io';
import 'package:dio/dio.dart';

main() async {
  var dio = Dio();
  dio.options.baseUrl = "http://www.dtworkroom.com/doris/1/2.0.0/";
  dio.options.connectTimeout = 5000; //5s
  dio.options.receiveTimeout = 5000;
  dio.options.headers = {'user-agent': 'dio', 'common-header': 'xx'};

// Or you can create dio instance and config it as follow:
// var dio = Dio(BaseOptions(
//    baseUrl: "http://www.dtworkroom.com/doris/1/2.0.0/",
//    connectTimeout: 5000,
//    receiveTimeout: 5000,
//    headers: {'user-agent': 'dio', 'common-header': 'xx'},
//  ));

  dio.interceptors
    ..add(InterceptorsWrapper(
      onRequest: (Options options) {
        // return ds.resolve( Response(data:"xxx"));
        // return ds.reject( DioError(message: "eh"));
        return options;
      },
    ))
    ..add(LogInterceptor(responseBody: false)); //Open log;

  Response response = await dio.get("https://www.google.com/");
  print(response.data);

  // Download a file
  response = await dio.download(
    "https://www.google.com/",
    "./example/xx.html",
    queryParameters: {"a": 1},
    onReceiveProgress: (received, total) {
      if (total != -1) {
        print('$received,$total');
      }
    },
  );

  // Create a FormData
  FormData formData = FormData.from({
    "name": "wendux",
    "age": 25,
    "file": UploadFileInfo(File("./example/upload.txt"), "upload.txt")
  });

  // Send FormData
  response = await dio.post("/test", data: formData);
  print(response);

  response = await dio.post(
    "/test",
    data: {
      "id": 8,
      "info": {"name": "wendux", "age": 25}
    },
    // Send data with "application/x-www-form-urlencoded" format
    options: Options(
      contentType: ContentType.parse("application/x-www-form-urlencoded"),
    ),
  );
  print(response.data);
}
1949
likes
0
pub points
99%
popularity

Publisher

flutterchina.club

A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.

Repository (GitHub)
View/report issues

License

LICENSE

Dependencies

cookie_jar

More

Packages that depend on dio