# std/cookies

Theme:

- Manual
- Standard library
- Index
- Compiler docs
- Fusion docs
- devel, stable

Search:
Source Edit

This module implements helper procs for parsing Cookies.

# Imports

strtabs, times, options

# Types

    SameSite {.pure.} = enum
      Default, None, Lax, Strict

  The SameSite cookie attribute. `Default` means that `setCookie` proc will not set `SameSite` attribute. Source Edit

# Procs

    proc parseCookies(s: string): StringTableRef {....raises: [], tags: [], forbids: [].}

  Parses cookies into a string table.

  The proc is meant to parse the Cookie header set by a client, not the "Set-Cookie" header set by servers.

  Example:

      import std/strtabs
      let cookieJar = parseCookies("a=1; foo=bar")
      assert cookieJar["a"] == "1"
      assert cookieJar["foo"] == "bar"

  Source Edit

    proc setCookie(key, value: string; domain = ""; path = ""; expires = "";
                   noName = false; secure = false; httpOnly = false;
                   maxAge = none(int); sameSite = SameSite.Default): string {.
        ...raises: [], tags: [], forbids: [].}

  Creates a command in the format of `Set-Cookie: key=value; Domain=...; ...`
  Tip: Cookies can be vulnerable. Consider setting `secure=true`, `httpOnly=true` and `sameSite=Strict`.
  Source Edit

    proc setCookie(key, value: string; expires: DateTime | Time; domain = "";
                   path = ""; noName = false; secure = false; httpOnly = false;
                   maxAge = none(int); sameSite = SameSite.Default): string

  Creates a command in the format of `Set-Cookie: key=value; Domain=...; ...` Source Edit