Ocookie is a dependency-light HTTP cookie toolkit for Dart and Flutter. It
parses Cookie request headers, parses and serializes Set-Cookie response
values, splits combined Set-Cookie headers, and provides a small client-side
cookie jar with pluggable storage.
- Request cookies: parse a
Cookieheader into name-value pairs. - Response cookies: parse and serialize individual
Set-Cookievalues. - Header compatibility: split combined
Set-Cookiestrings without breakingExpiresdates or quoted commas. - Client storage: normalize received cookies and build request
Cookieheaders for later URIs. - Extension points: customize value encoding and plug in your own
CookieStore.
dart pub add ocookieimport 'package:ocookie/ocookie.dart';| Task | API |
|---|---|
Parse a request Cookie header |
Cookie.parse |
Parse one response Set-Cookie value |
Cookie.fromString |
Construct a Set-Cookie value |
Cookie(...).serialize() |
| Check serialization errors without throwing | Cookie.validate |
Split a combined Set-Cookie string |
Cookie.splitSetCookie |
| Normalize one received cookie for matching | StoredCookie.fromSetCookie |
| Store response cookies and build request headers | CookieJar |
| Persist cookies outside memory | CookieStore |
Use Cookie.parse for the Cookie header sent from a client to a server.
final cookies = Cookie.parse('sid=abc; theme=light');
print(cookies['sid']); // abc
print(cookies['theme']); // lightMalformed segments and empty names are ignored. If the same name appears more than once, the first parsed value is kept.
Use Cookie.fromString when you receive one Set-Cookie value.
final cookie = Cookie.fromString(
'sid=abc; Path=/; HttpOnly; Secure; SameSite=None',
);
print(cookie.name); // sid
print(cookie.value); // abc
print(cookie.path); // /
print(cookie.httpOnly); // trueUse Cookie and serialize when you need to create a Set-Cookie value.
final cookie = Cookie(
'sid',
'abc',
maxAge: const Duration(hours: 1),
path: '/',
httpOnly: true,
secure: true,
sameSite: CookieSameSite.lax,
);
final headerValue = cookie.serialize();
print(headerValue);
// sid=abc; Max-Age=3600; Path=/; HttpOnly; Secure; SameSite=LaxSupported attributes include Expires, Max-Age, Domain, Path,
HttpOnly, Secure, Priority, SameSite, and Partitioned.
serialize validates cookie-safe characters and security constraints. For
example, SameSite=None and Partitioned both require Secure.
Some HTTP clients expose multiple Set-Cookie headers as one string. Splitting
on every comma is incorrect because Expires dates and quoted values can also
contain commas.
final values = Cookie.splitSetCookie(
'a=b; Expires=Wed, 21 Oct 2015 07:28:00 GMT, c=d; Path=/',
);
print(values.length); // 2
print(values.first); // a=b; Expires=Wed, 21 Oct 2015 07:28:00 GMT
print(values.last); // c=d; Path=/CookieJar is the high-level client API. Save response cookies with the URI
that produced them, then ask the jar for the Cookie request header for a later
URI.
final jar = CookieJar();
final now = DateTime.utc(2026, 1, 1);
await jar.save(
Uri.parse('https://example.com/login'),
[
'sid=abc; Path=/; HttpOnly',
'theme=dark; Path=/account',
],
now: now,
);
final header = await jar.header(
Uri.parse('https://example.com/account/settings'),
now: now,
);
print(header); // theme=dark; sid=abcThe jar applies host-only and domain matching, path matching, expiry handling,
secure-cookie matching, __Secure- and __Host- prefix constraints, and
request-header ordering by path length and creation time.
The default store is MemoryCookieStore. Provide a custom CookieStore when
cookies need to live in a database, file, secure storage, or another process.
Use validate when you want all serialization problems at once instead of the
first exception from serialize.
final cookie = Cookie(
'sid',
'abc',
sameSite: CookieSameSite.none,
);
final errors = cookie.validate();
if (errors.isNotEmpty) {
print(errors);
}Use copyWith to adjust a cookie while preserving the rest of its attributes.
Nullable attributes can be removed explicitly with clear.
final original = Cookie(
'sid',
'abc',
path: '/demo',
secure: true,
);
final moved = original.copyWith(path: '/next');
final withoutPath = original.copyWith(
clear: {CookieNullableField.path},
);Cookie values are URI component encoded by default. Pass custom codecs when an application already stores cookie-safe values or uses a different escaping scheme.
final serialized = Cookie(
'token',
'abc',
).serialize(encode: (value) => value.toUpperCase());
final parsed = Cookie.parse(
'token=ABC',
decode: (value) => value.toLowerCase(),
);
print(serialized); // token=ABC
print(parsed['token']); // abcThe runnable package example is in example/main.dart.
See the API documentation for the full public API.