HTTP::Cookies
Inherits Enumerable / Reference / Object
Represents a collection of cookies as it can be present inside a HTTP request or response.
NOTE: To use Cookies, you must explicitly import it with require "http/cookie"
Constructors
Creates a new instance by parsing the Cookie headers in the given HTTP::Headers.
See HTTP::Client::Response#cookies.
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.
Creates a new instance by parsing the Set-Cookie headers in the given HTTP::Headers.
See HTTP::Request#cookies.
Instance methods
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)
Returns true if this reference is the same as other. Invokes same?.
Gets the current HTTP::Cookie for the given key.
request.cookies["foo"].value # => "bar"
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 no path.
require "http/client"
request = HTTP::Request.new "GET", "/"
request.cookies["foo"] = "bar"
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)
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"
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.
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.
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.
Filling cookies by parsing the Cookie headers in the given HTTP::Headers.
Filling cookies by parsing the Cookie and Set-Cookie
headers in the given HTTP::Headers.
Filling cookies by parsing the Set-Cookie headers in the given HTTP::Headers.
Returns true if a cookie with the given key exists.
request.cookies.has_key?("foo") # => true
See Object#hash(hasher)
Returns a string representation of this cookies list.
It uses the Set-Cookie serialization from Cookie#to_set_cookie_header which
represents the full state of the cookie.
HTTP::Cookies{
HTTP::Cookie.new("foo", "bar"),
HTTP::Cookie.new("foo", "bar", domain: "example.com"),
}.to_s # => "HTTP::Cookies{\"foo=bar\", \"foo=bar; domain=example.com\"}"
Returns a string representation of this cookies list.
It uses the Set-Cookie serialization from Cookie#to_set_cookie_header which
represents the full state of the cookie.
HTTP::Cookies{
HTTP::Cookie.new("foo", "bar"),
HTTP::Cookie.new("foo", "bar", domain: "example.com"),
}.to_s # => "HTTP::Cookies{\"foo=bar\", \"foo=bar; domain=example.com\"}"
Returns a string representation of this cookies list.
It uses the Set-Cookie serialization from Cookie#to_set_cookie_header which
represents the full state of the cookie.
HTTP::Cookies{
HTTP::Cookie.new("foo", "bar"),
HTTP::Cookie.new("foo", "bar", domain: "example.com"),
}.to_s # => "HTTP::Cookies{\"foo=bar\", \"foo=bar; domain=example.com\"}"