data HTTP::Headers(entries: Array[HTTP::Field])

Header fields in order, names lowercased; a name may repeat. Names are case-insensitive, and written out in the conventional form (Content-Type). Immutable: put, add and delete return new headers.

h = HTTP::Headers.of({"Content-Type" => "text/plain"}).add("Set-Cookie", "a=1")
h["content-type"]          # => "text/plain"
h.add("set-cookie", "b=2").all("Set-Cookie")
# => ["a=1", "b=2"]
h.delete("set-cookie").key?("set-cookie")   # => false
HTTP::Headers.empty.add("bad name", "x")   # raises ArgumentError

<std:http>:107

Class methods

def self.empty -> HTTP::Headers

Headers with no field.

<std:http>:112

def self.of(pairs: Hash[String, String]) -> HTTP::Headers

Headers with a field for each pair, in order. Raises ArgumentError for a name that is not a token or a value with a control character, as #add does.

<std:http>:117

def self.field(name: String, value: String) -> HTTP::Field

A field that can be sent: its name a token, its value without CR, LF or another control character but HTAB (which would split the response). An ArgumentError otherwise, where the header is set; its message names the header but leaves the value out, which may come from a request (a cookie, a token) and would end up in logs.

<std:http>:126

def self.canonical(name: String) -> String

The conventional form of the field name name: each --separated word capitalized.

HTTP::Headers.canonical("content-type")      # => "Content-Type"
HTTP::Headers.canonical("X-FORWARDED-for")   # => "X-Forwarded-For"

<std:http>:195

Methods

def entries -> Array[HTTP::Field]

The fields, in order.

<std:http>:109

def with(entries: Array[HTTP::Field] = …) -> HTTP::Headers

A copy with the fields given replaced.

<std:http>:112

def to_h -> Hash[Symbol, Object]

The fields as a Hash, by name.

<std:http>:112

def [](name: String) -> String?

The first value of name.

<std:http>:133

def all(name: String) -> Array[String]

Every value of name, in order (empty when there is none).

<std:http>:140

def key?(name: String) -> Bool

Whether there is a field named name.

<std:http>:146

def put(name: String, value: String) -> HTTP::Headers

Every value of name replaced by value.

<std:http>:152

def add(name: String, value: String) -> HTTP::Headers

The headers with a field appended, keeping the other values of name. Raises ArgumentError for a name that is not a token or a value with CR, LF or another control character but HTAB (which would split the response); the message leaves the value out.

<std:http>:161

def delete(name: String) -> HTTP::Headers

The headers without any field named name.

<std:http>:166

def each(&: (String, String) -> Void) -> Void

Yields each field's name (lowercased) and value, in order.

<std:http>:172

def size -> Int

The number of fields, a repeated name counting each time.

<std:http>:177

def empty? -> Bool

Whether there is no field.

<std:http>:179

def token?(name: String, token: String) -> Bool

Whether a comma-separated header has token (case-insensitive): Connection: keep-alive, Upgrade.

<std:http>:183