Skip to content

Cookies ​

Read cookies from requests and set cookies on responses.

Reading cookies ​

cpp
app.get("/profile", [](xp::Request& req, xp::Response& res) {
    // Read a single cookie
    const auto token  = req.cookie("session_token");
    const auto userId = req.cookie("user_id");

    if (token.empty()) {
        throw xp::UnauthorizedError("Please log in.");
    }

    res.json({{"userId", userId}});
});

Read all cookies at once:

cpp
const auto cookies = req.cookies();  // unordered_map<string, string>
for (const auto& [name, value] : cookies) {
    std::cout << name << " = " << value << "\n";
}

Setting cookies ​

cpp
app.post("/login", [](xp::Request& req, xp::Response& res) {
    // Set a session cookie (expires when browser closes)
    res.cookie("session_id", "abc123");

    // Set a persistent cookie (expires in 7 days)
    res.cookie(
        "user_pref",     // name
        "dark-mode",     // value
        7 * 24 * 3600,   // max-age in seconds (7 days)
        true,            // HttpOnly
        false,           // Secure
        "Lax"            // SameSite
    );

    res.json({{"message", "Logged in"}});
});
ParameterTypeDefaultDescription
keystringrequiredCookie name
valuestringrequiredCookie value
max_age_secondsint00 = session cookie; positive value = persistent
http_onlybooltruePrevent JavaScript from reading the cookie
secureboolfalseOnly send over HTTPS
same_sitestring"Lax"CSRF protection: "Strict", "Lax", or "None"

Clearing cookies ​

cpp
app.post("/logout", [](xp::Request& req, xp::Response& res) {
    res.clearCookie("session_id");
    res.clearCookie("user_pref");
    res.json({{"message", "Logged out"}});
});

Security recommendations ​

GoalSetting
Prevent XSS from stealing cookiehttp_only = true
Prevent sending cookie over HTTPsecure = true (HTTPS only)
Prevent CSRFsame_site = "Strict" or "Lax"
Short-lived sessionSmall max_age_seconds

Best practice

Always set HttpOnly = true for session and auth cookies. This prevents JavaScript from stealing them via XSS attacks.

Built with ❤️ on top of Drogon, the world's fastest C++ HTTP framework.