Viewing version 4.0.1. Open the latest package page.

cookie_jar

4.0.1

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.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.ir
flutter pub add 'cookie_jar:4.0.1'
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.1
repository: https://github.com/flutterchina/cookie_jar
issue_tracker: https://github.com/flutterchina/cookie_jar/issues

environment:
  sdk: '>=2.12.0 <3.0.0'

dev_dependencies:
  lints: any
  test: ^1.16.3

Version history

VersionPublishedArchiveSHA-256
0.0.1Cached · Upstream SHA-256 matchedDownload7e06aaa8…16fd9c
0.0.2Cached · Upstream SHA-256 matchedDownload732e36e6…f05e35
0.0.3Cached · Upstream SHA-256 matchedDownloadbc2e43f4…1df818
0.0.4Cached · Upstream SHA-256 matchedDownload87f64b1b…68acc6
0.0.5Cached · Upstream SHA-256 matchedDownload18a3c99e…6dc57d
0.0.6Cached · Upstream SHA-256 matchedDownload8f7fe318…2262b3
0.0.7Cached · Upstream SHA-256 matchedDownloada03926fa…ba0a99
0.0.8Cached · Upstream SHA-256 matchedDownload0a76affc…555eba
1.0.0Cached · Upstream SHA-256 matchedDownloadc11415c0…998944
1.0.1Cached · Upstream SHA-256 matchedDownloadeede1c99…0d31e0
2.0.0-nullsafetyCached · Upstream SHA-256 matchedDownloadecca4ac2…03856f
2.0.0Cached · Upstream SHA-256 matchedDownload6a70766d…3bf485
3.0.0-beta1Cached · Upstream SHA-256 matchedDownloadb59f1302…bd91af
3.0.0-beta2Cached · Upstream SHA-256 matchedDownload6bc2af0e…7e9694
3.0.0Cached · Upstream SHA-256 matchedDownload38453776…2d9a13
3.0.1-prev1Cached · Upstream SHA-256 matchedDownload8b22e888…031d0d
3.0.1Cached · Upstream SHA-256 matchedDownloadd1cc6516…ec8a1d
4.0.0Cached · Upstream SHA-256 matchedDownloada0be140b…5c459e
4.0.1-betaCached · Upstream SHA-256 matchedDownload985d5e48…e51fe1
4.0.1Cached · Upstream SHA-256 matchedDownload25392054…a27815
4.0.2Cached · Upstream SHA-256 matchedDownload700cda09…f93bcc
4.0.3Cached · Upstream SHA-256 matchedDownload823fd13d…2f8ef9
4.0.4Cached · Upstream SHA-256 matchedDownloadb1755208…c4bf36
4.0.5Cached · Upstream SHA-256 matchedDownload1024f3a5…aa5c37
4.0.6Cached · Upstream SHA-256 matchedDownload03be5ec8…023c44
4.0.7Cached · Upstream SHA-256 matchedDownload7528474d…bc1b67
4.0.8Cached · Upstream SHA-256 matchedDownloada6ac027d…5e52de
4.0.9Cached · Upstream SHA-256 matchedDownload963da02c…7e8fff

Dependencies

No direct dependencies declared.