p-hatan/ mirror

pub repository

mirror.p-hatan.com

cookie_jar

3.0.0

A cookie manager for http requests in Dart, by which you can deal with the complex cookie policy and persist cookies easily.

Download archive

Readme

CookieJar

build statud Pub support

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: 3.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   
  await cj.saveFromResponse(Uri.parse("https://www.baidu.com/"), cookies);
  //Get cookies  
  List<Cookie> results = await 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 files in "./cookies"
var cj = PersistCookieJar(
    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();
var tempPath = tempDir.path;
var cj = PersistCookieJar(
          ignoreExpires: true,
          storage: FileStorage(tempPath)
        );

Storage

Now, You can customize your own storage,for more details refer to the implementation of FileStorage

APIs

Future saveFromResponse(Uri uri, List cookies);

Save the cookies for specified uri.

Future<List> loadForRequest(Uri uri);

Load the cookies for specified uri.

Future 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.

Future deleteAll();

Delete all cookies 。

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(await cj.loadForRequest(uri));
response = await request.close();
await 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/");
}

Note: cookieJar 3.0 need:

  • dio version >= 4.0
  • dio_cookie_manager >= 3.0

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

3.0.0

  • Change all APIs to async
  • Add delete 、 deleteAll APIs in CookieJar class.
  • Support custom cookie persistent storage。

2.0.1

  • support nullsafety

1.0.1

  • add forceInit method for PersistCookieJar
  • handle cookie name conflicts

0.0.1

  • Initial version, created by Stagehand

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add cookie_jar
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: 3.0.0
homepage: https://github.com/flutterchina/cookie_jar

environment:
  sdk: '>=2.12.0 <3.0.0'

dev_dependencies:
  test: ^1.16.3
  pedantic: ^1.10.0

Version history

VersionPublishedArchiveSHA-256
0.0.12018-04-26T10:09:09.764531ZDownload7e06aaa8e606a8ad4ad3b6885197f2935bb19a0cdb4c32944fc16131e816fd9c
0.0.22018-04-26T10:31:31.557650ZDownload732e36e6b192bb19cafab3247c7ddba6e5855faf5cc7315b5028736ca4f05e35
0.0.32018-04-27T08:03:23.511763ZDownloadbc2e43f4ec7628b7cb00d9b11b9d59d3fcb59f74fb42642e04d5978dc71df818
0.0.42018-05-23T08:47:24.984719ZDownload87f64b1b6641fb5f1876d9fb8c17dbadf4aa2eda0b533879c49181c0d668acc6
0.0.52018-08-20T02:23:42.893373ZDownload18a3c99e333bbd4b8ec562a5ac9e261091b53e6a9aa764f3aecc11dcf76dc57d
0.0.62018-08-20T03:21:09.413154ZDownload8f7fe31841b3d3e5980b5ec9b72b4d06f5b44e2ba4f37ce183a106afdb2262b3
0.0.72018-11-19T06:40:45.758239ZDownloada03926fa0700bbddbb8a75409d193ce42e3f44be4ab48a0973fa822151ba0a99
0.0.82019-02-17T13:32:22.168709ZDownload0a76affc41632e8b2409d1730e231422895c930d349ce21dd44f1ef8e4555eba
1.0.02019-02-18T07:23:23.324486ZDownloadc11415c0c8cbadec72269019b2269f50770b2233d3ffdc5c4bdd3c9067998944
1.0.12019-07-02T10:53:33.378918ZDownloadeede1c99d75847599b08911f680beb29e2b52aafad9a81271207f4dd8b0d31e0
2.0.0-nullsafety2021-02-22T13:27:31.218644ZDownloadecca4ac2d82978fd17f73ec1dc5df3593b5177821290f6d7951b7977a303856f
2.0.02021-02-22T13:36:11.576665ZDownload6a70766dbec044718f1ac82a47afd8dc94a3e64b8415c1c898781b82f43bf485
3.0.0-beta12021-02-28T09:30:26.112595ZDownloadb59f13022b7e62cc66e170c56d16ca6e2dbb6eb33d9368c8ed69887d96bd91af
3.0.0-beta22021-03-01T09:32:39.734231ZDownload6bc2af0e9399662d7caa80b79c936c1f80a88fe4dca09df5dd21e1e3dc7e9694
3.0.02021-03-28T11:42:10.686898ZDownload38453776bb7544c59820196ee6fb29119be7eace2271d20d241e6517de2d9a13
3.0.1-prev12021-03-31T08:55:13.339230ZDownload8b22e88846cf111baff58241fd024aa89992955ec9a0bb4e6994eb0093031d0d
3.0.12021-04-01T03:10:14.892021ZDownloadd1cc6516a190ba667941f722b6365d202caff3dacb38de24268b8d6ff1ec8a1d
4.0.02023-03-22T03:38:31.158358ZDownloada0be140b090d7458f8bfd4e821d97b8c4692177f3dd2981a47187fe1495c459e
4.0.1-beta2023-02-24T08:09:50.849744ZDownload985d5e483b74b87b12261a777dd2becb104a9267ab9258edf2da15bccae51fe1
4.0.12023-03-24T05:50:28.555787ZDownload253920541bf248e4eda6eac5df6aa00c19c183e1fe333629925d8df666a27815
4.0.22023-04-30T05:33:39.956318ZDownload700cda09e47e90c749a3bcaf6daef3cdef4eca246db63eceabceeb29cef93bcc
4.0.32023-06-01T04:26:04.801190ZDownload823fd13d22c5cf0c846e8a31c9b276f768f423cca134990c22c33045462f8ef9
4.0.42023-06-16T04:22:19.429853ZDownloadb1755208c3faff43408c5df116e185b93b6afe977d2c7f99e524979d98c4bf36
4.0.52023-06-16T15:16:14.968099ZDownload1024f3a52bee181fc8f10f4a359085471587d7406323090a53a7171bc0aa5c37
4.0.62023-07-18T03:07:37.957977ZDownload03be5ec8693f481a7cd8f25bca196e101872d3ced2cdbcf2d402c1c871023c44
4.0.72023-07-18T04:34:27.764505ZDownload7528474d59580b102b27eb0db992b878b8c515d7b18881d5711498ad6ebc1b67
4.0.82023-07-22T20:31:29.766486ZDownloada6ac027d3ed6ed756bfce8f3ff60cb479e266f3b0fdabd6242b804b6765e52de
4.0.92026-02-27T15:36:18.001514ZDownload963da02c1ef64cb5ac20de948c9e5940aa351f1e34a12b1d327c83d85b7e8fff

Dependencies

No direct dependencies declared.