cookie v0.1.2 SetCookie
Link to this section Summary
Functions
Serialize a set-cookie
header to expire the cookie.
Parse a set-cookie
header, into key, value and attributes.
Serialize a cookie with options to format for set-cookie
header.
Link to this section Functions
Link to this function
expire(key, opts \\ [])
Serialize a set-cookie
header to expire the cookie.
Options are the same as serialize/3
minus :max_age
and :expires
.
Examples
# Expire a cookie value
iex> expire("foo")
"foo=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; max-age=0; HttpOnly"
# Expire a cookie with routing options
iex> expire("foo", secure: true, http_only: false)
"foo=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; max-age=0; secure"
Link to this function
parse(set_cookie_string)
Parse a set-cookie
header, into key, value and attributes.
Examples
# Will parse cookie content
iex> parse("foo=bar; path=/; HttpOnly")
...> |> Map.take([:key, :value])
%{key: "foo", value: "bar"}
# Will parse cookie attributes
iex> parse("foo=bar; path=/; HttpOnly")
...> |> Map.get(:attributes)
%{http_only: true, path: "/"}
# Will parse cookie with empty value
iex> parse("foo=; path=/; HttpOnly")
...> |> Map.take([:key, :value])
%{key: "foo", value: ""}
# Will parse domain attribute
iex> parse("foo=bar; domain=example.com")
...> |> Map.get(:attributes)
%{domain: "example.com"}
# Will parse secure attribute
iex> parse("foo=bar; secure")
...> |> Map.get(:attributes)
%{secure: true}
# Will parse max_age attribute
iex> parse("foo=bar; max-age=20")
...> |> Map.get(:attributes)
%{max_age: "20"}
# Will parse expires attribute
iex> parse("foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT")
...> |> Map.get(:attributes)
%{expires: "Thu, 01 Jan 1970 00:00:00 GMT"}
Link to this function
serialize(key, value, opts \\ [])
Serialize a cookie with options to format for set-cookie
header.
The cookie value is not automatically escaped. Therefore, if you
want to store values with comma, quotes, etc, you need to explicitly
escape them or use a function such as Base.encode64
when writing
and Base.decode64
when reading the cookie.
Options
:domain
- the domain the cookie applies to:max_age
- the cookie max-age, in seconds. Providing a value for this option will set both the max-age and expires cookie attributes:path
- the path the cookie applies to:http_only
- when false, the cookie is accessible beyond http:secure
- if the cookie must be sent only over https. Defaults to true when the connection is https:extra
- string to append to cookie. Use this to take advantage of non-standard cookie attributes.
Examples
# Encode cookie with default options
iex> serialize("foo", "bar")
"foo=bar; path=/; HttpOnly"
# Encode cookie with empty value
iex> serialize("foo", "")
"foo=; path=/; HttpOnly"
# encodes with :path option
iex> serialize("foo", "bar", path: "/baz")
"foo=bar; path=/baz; HttpOnly"
# encodes with :domain option
iex> serialize("foo", "bar", domain: "example.com")
"foo=bar; path=/; domain=example.com; HttpOnly"
# encodes with :secure option
iex> serialize("foo", "bar", secure: true)
"foo=bar; path=/; secure; HttpOnly"
# encodes with :http_only option, which defaults to true
iex> serialize("foo", "bar", http_only: false)
"foo=bar; path=/"
# encodes with :max_age
iex> start = {{2012, 9, 29}, {15, 32, 10}}
...> serialize("foo", "bar", %{max_age: 60, universal_time: start})
"foo=bar; path=/; expires=Sat, 29 Sep 2012 15:33:10 GMT; max-age=60; HttpOnly"
# encodes whith :extra option
iex> serialize("foo", "bar", extra: "SameSite=Lax")
"foo=bar; path=/; HttpOnly; SameSite=Lax"