Skip to content

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

Creates a new instance by parsing the Cookie headers in the given HTTP::Headers.

See HTTP::Client::Response#cookies.

View source

.from_headers(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.

View source

.from_server_headers(headers) : self

Creates a new instance by parsing the Set-Cookie headers in the given HTTP::Headers.

See HTTP::Request#cookies.

View source

.new

Creates a new empty instance.

View source

Methods

#<<(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)
View source

#[](key)

Gets the current HTTP::Cookie for the given key.

request.cookies["foo"].value # => "bar"
View source

#[]=(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"
View source

#[]=(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)
View source

#[]?(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"
View source

#add_request_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.

View source

#add_response_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.

View source

#clear

Clears the collection, removing all cookies.

View source

#delete(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.

View source

#each(& : Cookie -> )

Yields each HTTP::Cookie in the collection.

View source

#each

Returns an iterator over the cookies of this collection.

View source

#empty?

Whether the collection contains any cookies.

View source

#fill_from_client_headers(headers)

Filling cookies by parsing the Cookie headers in the given HTTP::Headers.

View source

#fill_from_headers(headers)

Filling cookies by parsing the Cookie and Set-Cookie headers in the given HTTP::Headers.

View source

#fill_from_server_headers(headers)

Filling cookies by parsing the Set-Cookie headers in the given HTTP::Headers.

View source

#has_key?(key)

Returns true if a cookie with the given key exists.

request.cookies.has_key?("foo") # => true
View source

#size

Returns the number of cookies contained in this collection.

View source

#to_h

Returns this collection as a plain Hash.

View source