struct

HTTP::Headers

Inherits Enumerable / Struct / Value / Object

A Hash-like object that holds HTTP headers.

Two headers are considered the same if their downcase representation is the same (in which _ is the downcase version of -).

NOTE: To use Headers, you must explicitly import it with require "http/headers"

Constructors

Instance methods

==(other : self) : Bool

Equality operator.

Returns true if other is equal to self.

Keys are matched case-insensitive. String values are treated equal to an array values with the same string as single element.

HTTP::Headers{"Foo" => "bar"} == HTTP::Headers{"Foo" => "bar"}   # => true
HTTP::Headers{"Foo" => "bar"} == HTTP::Headers{"foo" => "bar"}   # => true
HTTP::Headers{"Foo" => "bar"} == HTTP::Headers{"Foo" => ["bar"]} # => true
HTTP::Headers{"Foo" => "bar"} == HTTP::Headers{"Foo" => "baz"}   # => false
Source
[](key : String) : String
Source
[]=(key : String, value : String) : String
Source
[]=(key : String, value : Array(String)) : Array(String)
Source
[]?(key : String) : String | Nil
Source
add(key : String, value : String) : self

Adds a header with key and value to the header set. If a header with key already exists in the set, value is appended to the existing header.

require "http/headers"

headers = HTTP::Headers.new
headers.add("Connection", "keep-alive")
headers["Connection"] # => "keep-alive"
headers.add("Connection", "Upgrade")
headers["Connection"] # => "keep-alive,Upgrade"
Source
add(key : String, value : Array(String)) : self
Source
add?(key : String, value : String) : Bool
Source
add?(key : String, value : Array(String)) : Bool
Source
clear
Source
clone
Source
delete(key : String) : String | Nil
Source
dup

Returns a shallow copy of this object.

Because Value is a value type, this method returns self, which already involves a shallow copy of this object because value types are passed by value.

Source
each

Must yield this collection's elements to the block.

Source
empty?

Returns true if self does not contain any element.

([] of Int32).empty? # => true
([1]).empty?         # => false
[nil, false].empty?  # => false
  • #present? returns the inverse.
Source
fetch(key : String, default : String | Nil) : String | Nil
Source
fetch(key, &)
Source
get(key : String) : Array(String)
Source
get?(key : String) : Array(String) | Nil
Source
has_key?(key : String) : Bool
Source
hash(hasher)

See Object#hash(hasher)

Source
includes_word?(key : String, word : String) : Bool

Returns if among the headers for key there is some that contains word as a value. The word is expected to match between word boundaries (i.e. non-alphanumeric chars).

require "http/headers"

headers = HTTP::Headers{"Connection" => "keep-alive, Upgrade"}
headers.includes_word?("Connection", "Upgrade") # => true
Source
inspect(io : IO) : Nil

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"
Source
merge!(other) : self
Source
object_id
Source
pretty_print(pp)
Source
same?(other : HTTP::Headers) : Bool
Source
serialize(io : IO) : Nil

Serializes headers according to the HTTP protocol.

Prints a list of HTTP header fields in the format described in RFC 7230 §3.2, with each field terminated by a CRLF sequence ("\r\n").

The serialization does not include a double CRLF sequence at the end.

headers = HTTP::Headers{"foo" => "bar", "baz" => %w[qux qox]}
headers.serialize # => "foo: bar\r\nbaz: qux\r\nbaz: qox\r\n"
Source
serialize

Serializes headers according to the HTTP protocol.

Prints a list of HTTP header fields in the format described in RFC 7230 §3.2, with each field terminated by a CRLF sequence ("\r\n").

The serialization does not include a double CRLF sequence at the end.

headers = HTTP::Headers{"foo" => "bar", "baz" => %w[qux qox]}
headers.serialize # => "foo: bar\r\nbaz: qux\r\nbaz: qox\r\n"
Source
to_json(builder : JSON::Builder) : Nil
Source
to_s(io : IO) : Nil

Same as #inspect(io).

Source
to_yaml(builder : YAML::Nodes::Builder) : Nil
Source
valid_value?(value : String) : Bool
Source