パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v4.0.9

cookie_jar

Dart における HTTP リクエスト用のクッキー管理ツールで、クッキー方針および永続性を扱うのを支援します。

AndroidiOSmacOSWindowsLinux
30日間ダウンロード
24.7万
いいね数
105
Pub ポイント
160

パッケージをインストール

flutter pub add cookie_jar

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

HTTPリクエストのクッキーを管理するDartパッケージで、クッキーポリシーのスムーズな処理とセッション間での永続的保存を可能にします。HTTPクライアントと統合されており、ドメインおよびパスルールに基づいてクッキーを自動的に保存・取得・送信できます。ログインセッションやユーザー設定など、Webサービスとの状態付きインタラクションが必要なアプリケーションに最適です。Android、iOS、macOS、Windows、Linuxなど複数のプラットフォームをサポートしており、クッキー管理におけるクロスプラットフォームの一貫性を確保しています。

用途

  • Web APIにおけるセッションクッキーの管理
  • ユーザーのログイン状態を維持する
  • HTTPリクエストにおけるクッキー処理の自動化
  • ステートフルなWebインタラクションをサポートする
  • セキュアなクッキー保存を有効にする

主な機能

  • 自動的なクッキーの永続化
  • クロスプラットフォーム互換性
  • HTTP クライアントとの統合
  • ポリシーに基づくクッキー管理
  • セキュアなストレージメカニズム

適している対象

  • セッション管理が必要な Web API クライアント
  • 長期的なクッキー保存を必要とするアプリケーション
  • 認証済みWebサービスを構築する開発者
  • クロスプラットフォームのモバイルおよびデスクトップアプリ
  • 複雑なクッキー政策を持つアプリ

注意事項

  • HTTP クライアントインスタンスとの手動統合が必要です
  • カスタムストレージ用に追加の構成が必要になる場合があります
  • 追加の保護策がない限り、高セキュリティ環境には適していません
  • 基本的なクッキー操作に限定
  • 下位のHTTPクライアント実装に依存します

元の README

関連するソースリポジトリの 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.

カテゴリー

依存関係

依存関係一覧 2 件

ソースリポジトリ

このパッケージを使用しているプロジェクト