https://github.com/odroe/ocookie
🍪 Cookie and Set-Cookie parser and serializer
https://github.com/odroe/ocookie
cookie dart parser serializer set-cookie
Last synced: 3 months ago
JSON representation
🍪 Cookie and Set-Cookie parser and serializer
- Host: GitHub
- URL: https://github.com/odroe/ocookie
- Owner: odroe
- License: mit
- Created: 2020-04-24T15:43:06.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2026-02-13T15:58:17.000Z (4 months ago)
- Last Synced: 2026-02-14T00:25:40.983Z (4 months ago)
- Topics: cookie, dart, parser, serializer, set-cookie
- Language: Dart
- Homepage: https://pub.dev/packages/ocookie
- Size: 37.1 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 🍪 Ocookie
Cookie and Set-Cookie parser and serializer.
## Installation
To install `ocookie` add the following to your `pubspec.yaml`
```yaml
dependencies:
ocookie: latest
```
Alternatively, you can run the following command:
```bash
dart pub add ocookie
```
## Basic Usage
```dart
final cookie = Cookie('name', 'value');
print(cookie.serialize()); // name=value
print(Cookie.parse('a=b;b=c')); // {a: b, b: c}
final setCookie = Cookie.fromString(
'sid=abc; Path=/; HttpOnly; Secure; SameSite=None',
);
print(setCookie.path); // /
final values = Cookie.splitSetCookie(
'a=b; Expires=Wed, 21 Oct 2015 07:28:00 GMT, c=d; Path=/',
);
print(values); // [a=b; Expires=Wed, 21 Oct 2015 07:28:00 GMT, c=d; Path=/]
```
### Utils
- `Cookie.serialize` - Serialize a cookie instance to string.
- `Cookie.validate` - Validate a cookie and return all errors.
- `Cookie.parse` - Parse client-side `cookie` header map.
- `Cookie.fromString` - Parse a set-cookie string to Cookie instance.
- `Cookie.splitSetCookie` - Split a string of multiple set-cookie values into a set-cookie string list.
## CopyWith And Clear
```dart
final original = Cookie(
'sid',
'abc',
path: '/demo',
secure: true,
sameSite: CookieSameSite.none,
);
final updated = original.copyWith(path: '/next');
final cleared = original.copyWith(
clear: {CookieNullableField.path},
);
```
## Validation
```dart
final cookie = Cookie(
'sid',
'abc',
sameSite: CookieSameSite.none,
);
final errors = cookie.validate();
if (errors.isNotEmpty) {
print(errors);
}
```
## Security Constraints
- `SameSite=None` requires `Secure=true`.
- `Partitioned=true` requires `Secure=true`.
## Flag Semantics
- `HttpOnly`, `Secure`, and `Partitioned` are two-state flags.
- Omitting a flag is equivalent to setting it to `false`.
# API Reference
See the [API documentation](https://pub.dev/documentation/ocookie) for detailed information about all available APIs.
## License
[MIT License](LICENSE)