request_cookie

Attribute Macro request_cookie 

Source
#[request_cookie]
Expand description

Extracts a specific cookie value or all cookies into a variable with panic on missing value.

This attribute macro supports two syntaxes:

  1. cookie(key => variable_name) - Extract a specific cookie value by key, panics if missing
  2. cookie(variable_name) - Extract all cookies as a raw string, panics if missing

§Usage

use hyperlane::*;
use hyperlane_macros::*;

#[route("/cookie")]
struct Cookie;

impl ServerHook for Cookie {
    async fn new(_ctx: &Context) -> Self {
        Self
    }

    #[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
    #[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
    async fn handle(self, ctx: &Context) {}
}

impl Cookie {
    #[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
    #[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
    async fn request_cookie_with_ref_self(&self, ctx: &Context) {}
}

#[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
#[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
async fn standalone_request_cookie_handler(ctx: &Context) {}

For specific cookie extraction, the variable will be available as String. For all cookies extraction, the variable will be available as String.

§Panics

This macro will panic if the requested cookie does not exist in the HTTP request headers.