NAME
PAGI::FastAPI::Cookies - Request Cookie Parsing Helper for PAGI::FastAPI
VERSION
Version v1.2.5
SYNOPSIS
use PAGI::FastAPI::Cookies qw(cookie parse_cookies);
$app->get('/whoami',
handler => async sub ($c) {
my $session_id = cookie($c, 'session_id');
return { session_id => $session_id // 'none' };
}
);
# Or parse once and read several values:
$app->get('/prefs',
handler => async sub ($c) {
my $cookies = parse_cookies($c->header('Cookie'));
return {
theme => $cookies->{theme} // 'light',
lang => $cookies->{lang} // 'en',
};
}
);
FUNCTIONS
parse_cookies($raw_header)
Parses a raw Cookie: header string into a plain HashRef of name => value. Values are percent-decoded. Returns an empty HashRef (never undef) if $raw_header is undefined or empty, so callers can safely chain ->{...} without a definedness check.
cookie($c, $name)
Convenience wrapper: parse_cookies($c->header('Cookie'))->{$name}. Returns undef if the cookie isn't present. Re-parses the header on every call, fine for reading one or two cookies, but call parse_cookies() once yourself if you need several values from the same request.
SETTING RESPONSE COOKIES
Not this module's concern, PAGI::FastAPI::Context already supports it directly, with zero extra code needed:
$c->add_header('set-cookie' => 'session=abc123; Path=/; HttpOnly; SameSite=Lax');
add_header (rather than set_header) is important if you need to set more than one cookie in the same response, for the same reason PAGI::FastAPI::Middleware::RateLimit uses it for stacked x-ratelimit-* headers, set_header would silently overwrite an earlier Set-Cookie value instead of adding a second one.
SEE ALSO
AUTHOR
Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>
REPOSITORY
https://github.com/manwar/PAGI-FastAPI
BUGS
Please report any bugs or feature requests through the web interface at https://github.com/manwar/PAGI-FastAPI/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc PAGI::FastAPI::Cookies
You can also look for information at:
BUG Report
Search MetaCPAN
LICENSE AND COPYRIGHT
Copyright (C) 2026 Mohammad Sajid Anwar.
This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License (2.0).