class ActionDispatch::Http::Headers
headers # => “token”<br>headers # => nil
# X_Custom_Header: token
# User-Agent: curl/7.43.0
# …
# GET / HTTP/1.1
dashes have to be interpreted as if they were originally sent as dashes.
ambiguity cannot be resolved at this stage anymore. Both underscores and
server, both dashes and underscores are converted to underscores. This
Also note that when headers are mapped to CGI-like variables by the Rack<br><br>headers # => “curl/7.43.0”<br>headers # => “text/plain”
headers = ActionDispatch::Http::Headers.from_hash(env)
env = { “CONTENT_TYPE” => “text/plain”, “HTTP_USER_AGENT” => “curl/7.43.0” }
Provides access to the request’s HTTP headers from the environment.
= Action Dispatch HTTP Headers
def self.from_hash(hash)
def self.from_hash(hash) new ActionDispatch::Request.new hash end
def [](key)
def [](key) @req.get_header env_name(key) end
def []=(key, value)
def []=(key, value) @req.set_header env_name(key), value end
def add(key, value)
def add(key, value) @req.add_header env_name(key), value end
def each(&block)
def each(&block) @req.each_header(&block) end
def env; @req.env.dup; end
def env; @req.env.dup; end
def env_name(key)
Converts an HTTP header name to an environment variable name if it is
def env_name(key) key = key.to_s if HTTP_HEADER.match?(key) key = key.upcase key.tr!("-", "_") key.prepend("HTTP_") unless CGI_VARIABLES.include?(key) end key end
def fetch(key, default = DEFAULT)
If the code block is provided, then it will be run and
raises a KeyError exception.
If the key is not found and an optional code block is not provided,
Returns the value for the given key mapped to @env.
def fetch(key, default = DEFAULT) @req.fetch_header(env_name(key)) do return default unless default == DEFAULT return yield if block_given? raise KeyError, key end end
def initialize(request) # :nodoc:
def initialize(request) # :nodoc: @req = request end
def key?(key)
def key?(key) @req.has_header? env_name(key) end
def merge(headers_or_env)
Returns a new Http::Headers instance containing the contents of
def merge(headers_or_env) headers = @req.dup.headers headers.merge!(headers_or_env) headers end
def merge!(headers_or_env)
entries; duplicate keys are overwritten with the values from
Adds the contents of headers_or_env to original instance
def merge!(headers_or_env) headers_or_env.each do |key, value| @req.set_header env_name(key), value end end