Skip to content

Request cookies

Inject &RequestCookies into your components to access the cookies sent by the client alongside the incoming request. You can then retrieve a cookie by name using the get method:

src/core/routes.rs
use pavex::cookie::RequestCookies;
use pavex::response::Response;

pub fn handler(request_cookies: &RequestCookies) -> Response {
    let Some(session_id) = request_cookies.get("session_id") else {
        return Response::unauthorized();
    };
    // Further processing
    // [...]
}

Multiple cookies with the same name

In some scenarios, the client might send multiple cookies with the same name.
get will return the first one it finds. If that's not what you want, you can use get_all to retrieve all of them:

src/multiple/routes.rs
use pavex::cookie::RequestCookies;
use pavex::response::Response;

pub fn handler(request_cookies: &RequestCookies) -> Response {
    let cookies_values: Vec<_> = match request_cookies.get_all("origin") {
        Some(cookies) => cookies.values().collect(),
        None => Vec::new(),
    };
    // Further processing
    // [...]
}