HTTP 요청에 대한 쿠키를 관리하는 Dart 패키지로, 쿠키 정책 처리와 세션 간 지속 저장을 원활하게 가능하게 합니다. 도메인 및 경로 규칙에 따라 자동으로 쿠키를 저장하고 검색하며 전송할 수 있도록 HTTP 클라이언트와 통합되어 있습니다. 로그인 세션 또는 사용자 설정과 같은 웹 서비스와의 상태 기반 상호작용이 필요한 애플리케이션에 적합합니다. Android, iOS, macOS, Windows, Linux 등 여러 플랫폼을 지원하여 쿠키 관리에서 크로스플랫폼 일관성을 보장합니다.
사용 사례
웹 API에서 세션 쿠키 관리
사용자 로그인 상태 유지
HTTP 요청에서 쿠키 처리 자동화
상태 기반 웹 상호작용 지원
보안 쿠키 저장 사용
주요 기능
자동 쿠키 유지
크로스 플랫폼 호환성
HTTP 클라이언트와의 통합
정책 기반 쿠키 관리
안전한 저장 메커니즘
적합한 대상
세션 관리가 필요한 웹 API 클라이언트
장기적인 쿠키 저장을 필요로 하는 응용 프로그램
인증된 웹 서비스를 구축하는 개발자
크로스플랫폼 모바일 및 데스크톱 앱
복잡한 쿠키 정책을 가진 앱
고려 사항
HTTP 클라이언트 인스턴스와 수동 통합이 필요합니다
사용자 지정 스토리지에 대한 추가 구성이 필요할 수 있습니다
추가 보호 조치 없이 고보안 환경에는 적합하지 않습니다
기본 쿠키 작업에 한정
하위 HTTP 클라이언트 구현에 따라 다릅니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
CookieJar
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:
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 = awaitgetTemporaryDirectory();
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.
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';
voidmain() async {
final dio = Dio();
final cookieJar = CookieJar();
dio.interceptors.add(CookieManager(cookieJar));
await dio.get('https://pub.dev/');
// Print cookiesprint(await cookieJar.loadForRequest(Uri.parse('https://pub.dev/')));
// Another request with the cookie.await dio.get("https://pub.dev/");
}