Sending cookies to the client

Use the complete form of send(), that takes a x::http::responseimpl, to send cookies to the client.

void received(const x::http::requestimpl &req, bool hasbody)
    // ...

    x::http::responseimpl resp;

        "text/plain; charset=\"utf-8\"");

    resp.addCookie(x::http::cookie("loggedin", "yes")
                   .setExpiresIn(24 * 60 * 60));

    send(resp, req, buffer);

addCookie() adds a Set-Cookie header, based on the contents of the given x::http::cookie. Its constructor takes the cookie's name and value, and additional method set various cookie properties, such as the cookie's scope and expiration.


Cookie names and values must follow the constraints on the names and values of cookies. Cookie names must generally be alphanumeric, cookie values must consists of US-ASCII characters excepting control character, quotes, commas, semicolons, and backslashes.

x::http::requestimpl's getCookies() method retrieves any cookies sent in the client's request:

void received(const x::http::requestimpl &req, bool hasbody)
    std::map<std::string, std::string> cookies;

// ...

getCookies() loads the given map with the contents of any cookies in the request.