class

HTTP::Cookie

Inherits Reference / Object

Represents a cookie with all its attributes. Provides convenient access and modification of them.

NOTE: To use Cookie, you must explicitly import it with require "http/cookie"

Constructors

new(name : String, value : String, path : String | Nil = nil, expires : Time | Nil = nil, domain : String | Nil = nil, secure : Bool | Nil = nil, http_only : Bool = false, samesite : SameSite | Nil = nil, extension : String | Nil = nil, max_age : Time::Span | Nil = nil, creation_time : Time = Time.utc)

Creates a new Cookie instance.

Raises IO::Error if name or value are invalid as per RFC 6265 §4.1.1. Raises ArgumentError if name has a security prefix but the requirements are not met as per RFC 6265 bis §4.1.3. Alternatively, if name has a security prefix, and the related properties are nil, the prefix will automatically be applied to the cookie.

Source

Instance methods

==(other : self)

Returns true if this reference is the same as other. Invokes same?.

creation_time
Source
domain
Source
domain=(domain : String | Nil)
Source
expiration_time

Returns the expiration time of this cookie.

Source
expire

Expires the cookie.

Causes the cookie to be destroyed. Sets the value to the empty string and expires its lifetime.

cookie = HTTP::Cookie.new("hello", "world")
cookie.expire

cookie.value    # => ""
cookie.expired? # => true
Source
expired?(time_reference : Time = Time.utc) : Bool

Returns the expiration status of this cookie as a Bool.

time_reference can be passed to use a different reference time for comparison. Default is the current time (Time.utc).

Source
expires
Source
expires=(expires : Time | Nil)
Source
extension
Source
extension=(extension : String | Nil)
Source
hash(hasher)

See Object#hash(hasher)

http_only
Source
http_only=(http_only : Bool)
Source
inspect(io : IO) : Nil

Returns an unambiguous string representation of this cookie.

It uses the Set-Cookie serialization from #to_set_cookie_header which represents the full state of the cookie.

HTTP::Cookie.new("foo", "bar").inspect                        # => HTTP::Cookie["foo=bar"]
HTTP::Cookie.new("foo", "bar", domain: "example.com").inspect # => HTTP::Cookie["foo=bar; domain=example.com"]
Source
max_age
Source
max_age=(max_age : Time::Span | Nil)
Source
name
Source
name=(name : String) : Nil

Sets the name of this cookie.

Raises IO::Error if the value is invalid as per RFC 6265 §4.1.1.

Source
path
Source
path=(path : String | Nil)
Source
samesite
Source
samesite=(samesite : SameSite | Nil)
Source
secure

Returns true if this cookie has the Secure flag.

Source
secure=(secure : Bool) : Bool
Source
to_s(io : IO) : Nil

Returns a string representation of this cookie.

It uses the Set-Cookie serialization from #to_set_cookie_header which represents the full state of the cookie.

HTTP::Cookie.new("foo", "bar").to_s                        # => "foo=bar"
HTTP::Cookie.new("foo", "bar", domain: "example.com").to_s # => "foo=bar; domain=example.com"
Source
valid?

Returns false if #name has a security prefix but the requirements are not met as per RFC 6265 bis §4.1.3, otherwise returns true.

Source
validate!

Raises ArgumentError if self is not #valid?.

Source
value
Source
value=(value : String) : String

Sets the value of this cookie.

Raises IO::Error if the value is invalid as per RFC 6265 §4.1.1.

Source

Nested types