p-hatan/ mirror

pub repository

mirror.p-hatan.com

cookie_jar

4.0.8

A cookie manager for http requests in Dart, help you to deal with the cookie policies and persistence.

Download archive

Readme

CookieJar

Pub GitHub license

A cookie manager for http requests in Dart, help you to deal with the cookie policies and persistence.

Get started

Checkout the Migration Guide for breaking changes between versions.

Add dependency

You can use the command to add dio as a dependency with the latest stable version:

$ dart pub add cookie_jar

Or you can manually add cookie_jar into the dependencies section in your pubspec.yaml:

dependencies:
  cookie_jar: ^replace-with-latest-version

The latest version is: Pub The latest version including pre-releases is: Pub

Usage

A simple usage example:

import 'package:cookie_jar/cookie_jar.dart';

void main() async {
  final cookieJar = CookieJar();
  List<Cookie> cookies = [Cookie('name', 'wendux'), Cookie('location', 'china')];
  // Saving cookies.
  await cookieJar.saveFromResponse(Uri.parse('https://pub.dev/'), cookies);
  // Obtain cookies.
  List<Cookie> results = await cookieJar.loadForRequest(Uri.parse('https://pub.dev/paths'));
  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 cookie container and manager for HTTP requests.

DefaultCookieJar

DefaultCookieJar is a default cookie manager which implements the standard cookie policy declared in RFC. It saves the cookies in the memory, all cookies will be cleared after the app exited.

To use it:


final cookieJar = 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.

To use it:

// Cookie files will be saved in files in "./cookies/4/"
final cookieJar = PersistCookieJar(
  ignoreExpires: true, // Save/load even cookies that have expired.
);

Note: When using the [FileStorage] in Flutter apps, use path_provider to obtain available directories.


Directory tempDir = await

getTemporaryDirectory();

final tempPath = tempDir.path;
final cookieJar = PersistCookieJar(
  ignoreExpires: true,
  storage: FileStorage(tempPath),
);

Storage

You can customize your own storage by extending Storage, see FileStorage for more details.

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 ignore the uri.path.

If withDomainSharedCookie is true , all domain-shared cookies will be deleted.

Future deleteAll();

Delete all cookies.

Working with HttpClient

Using CookieJar or PersistCookieJar manages HttpClient 's request/response cookies is quite easy:

final cookieJar = CookieJar();
request = await httpClient.openUrl(options.method, uri);
request.cookies.addAll(await cj.loadForRequest(uri));
response = await request.close();
await cookieJar.saveFromResponse(uri, response.cookies);

Working with dio

dio is a powerful HTTP client for Dart/Flutter, which supports global configuration, interceptors, FormData, request cancellation, file uploading/downloading, timeout, and custom adapters etc. dio also supports to manage cookies with cookie_jar using dio_cookie_manager. For example:

import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:cookie_jar/cookie_jar.dart';

void main() async {
  final dio = Dio();
  final cookieJar = CookieJar();
  dio.interceptors.add(CookieManager(cookieJar));
  await dio.get('https://pub.dev/');
  // Print cookies
  print(await cookieJar.loadForRequest(Uri.parse('https://pub.dev/')));
  // Another request with the cookie.
  await dio.get("https://pub.dev/");
}

More details about dio.

Copyright & License

The project and it's underlying projects are originally authored by @wendux with the organization @flutterchina since 2018.

The project consents the MIT license.

Changelog

CHANGELOG

4.0.8

  • Simply replace \ to / rather than using Uri when parsing a storage base directory.

4.0.7

  • Allow to configure whether to delete host cookies when load failed.
  • Fix directory parsing for the file storage.
  • Add WebCookieJar to convenient Web usages instead of throwing exceptions.

4.0.5

  • Fixed problem with returning false if cookiePath end with '/'.

4.0.4

  • Export Cookie from universal_io.

4.0.3

  • Fix runtime type cast exception.

4.0.2

  • Use universal_io to handle imports. This also bumps the minimum required SDK version to 2.15.0.

4.0.1

  • Clean codes and update documents.
  • Support to compile on Web.

4.0.0

  • Preserve all cookies that with same name but in different paths when request (as Chrome acts). For example, in request header: Cookie: a=1; a=2; a=3. Note: Cookies with same name will be sorted by path (longer path first) when return (List<Cookie> that loadForRequest returned).
  • Fix path and domain match error (#30)
  • Fix #31: Use the "directory" of request-uri as default path if there is no 'Path' component in Set-Cookie line (in 3.x, used root '/', it is not standard).

3.0.1

  • refactor

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, help you to deal with the cookie policies and persistence.
version: 4.0.8
repository: https://github.com/flutterchina/cookie_jar
issue_tracker: https://github.com/flutterchina/cookie_jar/issues

environment:
  sdk: '>=2.15.0 <3.0.0'

dependencies:
  meta: ^1.5.0
  universal_io: ^2.2.0

dev_dependencies:
  lints: any
  test: ^1.16.3

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

meta "^1.5.0"

universal_io "^2.2.0"