Viewing version 2.0.0-nullsafety. Open the latest package page.
cookie_jar
2.0.0-nullsafetyA cookie manager for http requests in Dart, by which you can deal with the complex cookie policy and persist cookies easily.
Readme
CookieJar
A cookie manager for http requests in Dart, by which you can deal with the complex cookie policy and persist cookies easily.
Add dependency
dependencies:
cookie_jar: ^1.0.0
Usage
A simple usage example:
import 'package:cookie_jar/cookie_jar.dart';
void main() async {
List<Cookie> cookies = [Cookie("name", "wendux"),Cookie("location", "china")];
var cj = CookieJar();
//Save cookies
cj.saveFromResponse(Uri.parse("https://www.baidu.com/"), cookies);
//Get cookies
List<Cookie> results = cj.loadForRequest(Uri.parse("https://www.baidu.com/xx"));
print(results);
}
Classes
SerializableCookie
This class is a wrapper for Cookie class. Because the Cookie class doesn't support Json serialization, for the sake of persistence, we use this class instead of it.
CookieJar
CookieJar is a default cookie manager which implements the standard cookie policy declared in RFC. CookieJar saves the cookies in RAM, so if the application exit, all cookies will be cleared. A example as follow:
var cj= CookieJar();
PersistCookieJar
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. A example as follows:
// Cookie files will be saved in "./cookies"
var cj = PersistCookieJar(
dir:"./cookies",
ignoreExpires:true, //save/load even cookies that have expired.
);
Note: In Flutter, File system is different from PC, you can use path_provider package to get the path :
// API `getTemporaryDirectory` is from "path_provider" package. Directory tempDir = await getTemporaryDirectory(); String tempPath = tempDir.path; CookieJar cj=PersistCookieJar(dir:tempPath);
APIs
void saveFromResponse(Uri uri, List cookies);
Save the cookies for specified uri.
List loadForRequest(Uri uri);
Load the cookies for specified uri.
delete(Uri uri,[bool withDomainSharedCookie = false] )
Delete cookies for specified uri. This API will delete all cookies for the uri.host, it will ignored the uri.path.
If withDomainSharedCookie is true , will delete the domain-shared cookies.
Note: This API is only available in PersistCookieJar class.
Working with HttpClient
Using CookieJar or PersistCookieJar manages HttpClient 's request/response cookies is very easy:
var cj=CookieJar();
...
request = await httpClient.openUrl(options.method, uri);
request.cookies.addAll(cj.loadForRequest(uri));
response = await request.close();
cj.saveFromResponse(uri, response.cookies);
Working with dio
dio is a powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, File downloading, Timeout etc. And dio supports to manage cookies with cookie_jar, the simple example is:
import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:cookie_jar/cookie_jar.dart';
main() async {
var dio = Dio();
var cookieJar=CookieJar();
dio.interceptors.add(CookieManager(cookieJar));
await dio.get("https://baidu.com/");
// Print cookies
print(cookieJar.loadForRequest(Uri.parse("https://baidu.com/")));
// second request with the cookie
await dio.get("https://baidu.com/");
}
More details about dio see : https://github.com/flutterchina/dio .
Copyright & License
This open source project authorized by https://flutterchina.club , and the license is MIT.
Features and bugs
Please file feature requests and bugs at the issue tracker.
Changelog
2.0.0-nullsafety
- support nullsafety
1.0.1
- add
forceInitmethod for PersistCookieJar - handle cookie name conflicts
0.0.1
- Initial version, created by Stagehand
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'cookie_jar:2.0.0-nullsafety'pubspec.yaml
name: cookie_jar
description: A cookie manager for http requests in Dart, by which you can deal with the complex cookie policy and persist cookies easily.
version: 2.0.0-nullsafety
homepage: https://github.com/flutterchina/cookie_jar
environment:
sdk: '>=2.12.0-29.10.beta <3.0.0'
#dependencies:
# path: ^1.4.1
dev_dependencies:
test: ^1.16.0-nullsafety
pedantic: ^1.10.0-nullsafety
# coverage: "^0.12.4"
# dart_codecov_generator:
# git:
# url: "https://github.com/wendux/dart-codecov.git"
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | Cached · Upstream SHA-256 matched | Download | 7e06aaa8…16fd9c |
| 0.0.2 | Cached · Upstream SHA-256 matched | Download | 732e36e6…f05e35 |
| 0.0.3 | Cached · Upstream SHA-256 matched | Download | bc2e43f4…1df818 |
| 0.0.4 | Cached · Upstream SHA-256 matched | Download | 87f64b1b…68acc6 |
| 0.0.5 | Cached · Upstream SHA-256 matched | Download | 18a3c99e…6dc57d |
| 0.0.6 | Cached · Upstream SHA-256 matched | Download | 8f7fe318…2262b3 |
| 0.0.7 | Cached · Upstream SHA-256 matched | Download | a03926fa…ba0a99 |
| 0.0.8 | Cached · Upstream SHA-256 matched | Download | 0a76affc…555eba |
| 1.0.0 | Cached · Upstream SHA-256 matched | Download | c11415c0…998944 |
| 1.0.1 | Cached · Upstream SHA-256 matched | Download | eede1c99…0d31e0 |
| 2.0.0-nullsafety | Cached · Upstream SHA-256 matched | Download | ecca4ac2…03856f |
| 2.0.0 | Cached · Upstream SHA-256 matched | Download | 6a70766d…3bf485 |
| 3.0.0-beta1 | Cached · Upstream SHA-256 matched | Download | b59f1302…bd91af |
| 3.0.0-beta2 | Cached · Upstream SHA-256 matched | Download | 6bc2af0e…7e9694 |
| 3.0.0 | Cached · Upstream SHA-256 matched | Download | 38453776…2d9a13 |
| 3.0.1-prev1 | Cached · Upstream SHA-256 matched | Download | 8b22e888…031d0d |
| 3.0.1 | Cached · Upstream SHA-256 matched | Download | d1cc6516…ec8a1d |
| 4.0.0 | Cached · Upstream SHA-256 matched | Download | a0be140b…5c459e |
| 4.0.1-beta | Cached · Upstream SHA-256 matched | Download | 985d5e48…e51fe1 |
| 4.0.1 | Cached · Upstream SHA-256 matched | Download | 25392054…a27815 |
| 4.0.2 | Cached · Upstream SHA-256 matched | Download | 700cda09…f93bcc |
| 4.0.3 | Cached · Upstream SHA-256 matched | Download | 823fd13d…2f8ef9 |
| 4.0.4 | Cached · Upstream SHA-256 matched | Download | b1755208…c4bf36 |
| 4.0.5 | Cached · Upstream SHA-256 matched | Download | 1024f3a5…aa5c37 |
| 4.0.6 | Cached · Upstream SHA-256 matched | Download | 03be5ec8…023c44 |
| 4.0.7 | Cached · Upstream SHA-256 matched | Download | 7528474d…bc1b67 |
| 4.0.8 | Cached · Upstream SHA-256 matched | Download | a6ac027d…5e52de |
| 4.0.9 | Cached · Upstream SHA-256 matched | Download | 963da02c…7e8fff |
Dependencies
No direct dependencies declared.