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"
Creates a new instance by parsing the Cookie headers in the given HTTP::Headers.
Creates a new instance by parsing the Cookie and Set-Cookie headers in the given HTTP::Headers.
DEPRECATED Use .from_client_headers or .from_server_headers instead.
Creates a new instance by parsing the Set-Cookie headers in the given HTTP::Headers.
Creates a new empty instance.
Adds the given cookie to this collection, overrides an existing cookie with the same name if present.
Returns true if this reference is the same as other.
Gets the current HTTP::Cookie for the given key.
Sets a new cookie in the collection with a string value.
Sets a new cookie in the collection to the given HTTP::Cookie instance.
Gets the current HTTP::Cookie for the given key or nil if none is set.
Adds Cookie headers for the cookies in this collection to the given HTTP::Headers instance and returns it.
Adds Set-Cookie headers for the cookies in this collection to the given HTTP::Headers instance and returns it.
Clears the collection, removing all cookies.
Deletes and returns the HTTP::Cookie for the specified key, or returns nil if key cannot be found in the collection.
Yields each HTTP::Cookie in the collection.
Returns an iterator over the cookies of this collection.
Whether the collection contains any cookies.
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.
DEPRECATED Use #fill_from_client_headers or #fill_from_server_headers instead.
Filling cookies by parsing the Set-Cookie headers in the given HTTP::Headers.
Returns true if a cookie with the given key exists.
Returns a string representation of this cookies list.
Returns a string representation of this cookies list.
Returns the number of cookies contained in this collection.
Returns this collection as a plain Hash.
Returns a string representation of this cookies list.
Enumerable(HTTP::Cookie)
Enumerable(HTTP::Cookie)
Reference
Reference
Reference
Object
Object
Object
Creates a new instance by parsing the Cookie headers in the given HTTP::Headers.
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.
DEPRECATED Use .from_client_headers or .from_server_headers instead.
Creates a new instance by parsing the Set-Cookie headers in the given HTTP::Headers.
Creates a new empty instance.
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.
Yields each HTTP::Cookie in the collection.
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.
DEPRECATED Use #fill_from_client_headers or #fill_from_server_headers instead.
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 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\"}"
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/HTTP/Cookies.html