패키지, 저장소 및 앱 카테고리 검색

패키지 상세v4.0.9

cookie_jar

Dart에서 HTTP 요청을 위한 쿠키 관리자로, 쿠키 정책 및 지속성을 다루는 데 도움을 줍니다.

AndroidiOSmacOSWindowsLinux
30일 다운로드
24.7만
좋아요
105
Pub 점수
160

패키지 설치

flutter pub add cookie_jar

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

HTTP 요청에 대한 쿠키를 관리하는 Dart 패키지로, 쿠키 정책 처리와 세션 간 지속 저장을 원활하게 가능하게 합니다. 도메인 및 경로 규칙에 따라 자동으로 쿠키를 저장하고 검색하며 전송할 수 있도록 HTTP 클라이언트와 통합되어 있습니다. 로그인 세션 또는 사용자 설정과 같은 웹 서비스와의 상태 기반 상호작용이 필요한 애플리케이션에 적합합니다. Android, iOS, macOS, Windows, Linux 등 여러 플랫폼을 지원하여 쿠키 관리에서 크로스플랫폼 일관성을 보장합니다.

사용 사례

  • 웹 API에서 세션 쿠키 관리
  • 사용자 로그인 상태 유지
  • HTTP 요청에서 쿠키 처리 자동화
  • 상태 기반 웹 상호작용 지원
  • 보안 쿠키 저장 사용

주요 기능

  • 자동 쿠키 유지
  • 크로스 플랫폼 호환성
  • HTTP 클라이언트와의 통합
  • 정책 기반 쿠키 관리
  • 안전한 저장 메커니즘

적합한 대상

  • 세션 관리가 필요한 웹 API 클라이언트
  • 장기적인 쿠키 저장을 필요로 하는 응용 프로그램
  • 인증된 웹 서비스를 구축하는 개발자
  • 크로스플랫폼 모바일 및 데스크톱 앱
  • 복잡한 쿠키 정책을 가진 앱

고려 사항

  • 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 개

소스 저장소

이 패키지를 사용하는 프로젝트