class HTTP::Cookies
inherits Reference
¶
Represents a collection of cookies as it can be present inside a HTTP request or response.
Included modules
Enumerable
Class methods¶
.from_client_headers(headers) : self
¶
(headers) : self
Creates a new instance by parsing the Cookie
headers in the given HTTP::Headers
.
.from_headers(headers) : self
¶
(headers) : self
Creates a new instance by parsing the Cookie
and Set-Cookie
headers in the given HTTP::Headers
.
See HTTP::Request#cookies
and HTTP::Client::Response#cookies
.
.from_server_headers(headers) : self
¶
(headers) : self
Creates a new instance by parsing the Set-Cookie
headers in the given HTTP::Headers
.
Methods¶
#<<(cookie : Cookie)
¶
(cookie : Cookie)
Adds the given cookie to this collection, overrides an existing cookie with the same name if present.
response.cookies << HTTP::Cookie.new("foo", "bar", http_only: true)
#[](key)
¶
(key)
Gets the current HTTP::Cookie
for the given key.
request.cookies["foo"].value # => "bar"
#[]=(key, value : String)
¶
(key, value : String)
Sets a new cookie in the collection with a string value.
This creates a never expiring, insecure, not HTTP only cookie with
no explicit domain restriction and the path /
.
require "http/client"
request = HTTP::Request.new "GET", "/"
request.cookies["foo"] = "bar"
#[]=(key, value : Cookie)
¶
(key, value : Cookie)
Sets a new cookie in the collection to the given HTTP::Cookie
instance. The name attribute must match the given key, else
ArgumentError
is raised.
require "http/client"
response = HTTP::Client::Response.new(200)
response.cookies["foo"] = HTTP::Cookie.new("foo", "bar", "/admin", Time.utc + 12.hours, secure: true)
#[]?(key)
¶
(key)
Gets the current HTTP::Cookie
for the given key or nil
if none is set.
require "http/client"
request = HTTP::Request.new "GET", "/"
request.cookies["foo"]? # => nil
request.cookies["foo"] = "bar"
request.cookies["foo"]?.try &.value # > "bar"
#add_request_headers(headers)
¶
(headers)
Adds Cookie
headers for the cookies in this collection to the
given HTTP::Headers
instance and returns it. Removes any existing
Cookie
headers in it.
#add_response_headers(headers)
¶
(headers)
Adds Set-Cookie
headers for the cookies in this collection to the
given HTTP::Headers
instance and returns it. Removes any existing
Set-Cookie
headers in it.
#delete(key)
¶
(key)
Deletes and returns the HTTP::Cookie
for the specified key, or
returns nil
if key cannot be found in the collection. Note that
key should match the name attribute of the desired HTTP::Cookie
.
#fill_from_client_headers(headers)
¶
(headers)
Filling cookies by parsing the Cookie
headers in the given HTTP::Headers
.
#fill_from_headers(headers)
¶
(headers)
Filling cookies by parsing the Cookie
and Set-Cookie
headers in the given HTTP::Headers
.
#fill_from_server_headers(headers)
¶
(headers)
Filling cookies by parsing the Set-Cookie
headers in the given HTTP::Headers
.
#has_key?(key)
¶
(key)
Returns true
if a cookie with the given key exists.
request.cookies.has_key?("foo") # => true