dio_http_cookie_manager 3.0.0
dio_http_cookie_manager: ^3.0.0 copied to clipboard
A cookie manager for Dio, which supports persistent cookies in RAM and file.
dio_http_cookie_manager
#
A cookie manager for dio_http.
Getting Started #
Install #
dependencies:
dio_http_cookie_manager: ^3.0.0 #latest version
Usage #
import 'package:dio_http/dio_http.dart';
import 'package:dio_http_cookie_manager/dio_http_cookie_manager.dart';
import 'package:cookie_jar/cookie_jar.dart';
main() async {
var dio = Dio();
var cookieJar=CookieJar();
dio.interceptors.add(CookieManager(cookieJar));
// first request, and save cookies (CookieManager do it).
await dio.get("https://baidu.com/");
// Print cookies
// print(await cookieJar.loadForRequest(Uri.parse("https://baidu.com/")));
// second request with the cookies
await dio.get("https://baidu.com/");
...
}
Cookie Manager #
CookieManager Interceptor can help us manage the request/response cookies automatically. CookieManager depends on cookieJar package :
The dio_http_cookie_manager manage API is based on the withdrawn cookie_jar.
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, for example:
dio.interceptors.add(CookieManager(PersistCookieJar()))
PersistCookieJar persists the cookies in files, so if the application exit, the cookies always exist unless call delete explicitly.
Note: In flutter, the path passed to
PersistCookieJarmust be valid(exists in phones and with write access). you can use path_provider package to get right path.
In flutter:
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
var cj = PersistCookieJar(ignoreExpires: true, storage: FileStorage(appDocPath +"/.cookies/" ));
dio.interceptors.add(CookieManager(cj));
...