HTTP headers are name/value pairs that describe a request.

headers Normalizing header names makes lookup predictable even when callers use different capitalization.

Request Headers

accept
request_headers.rb
Replay: real traced execution (multi-file project)
accept = "application/json"
headers = {
  "Host" => "example.com",
  "Accept" => accept,
  "User-Agent" => "egtry-demo"
}

normalized = headers.transform_keys { |key| key.downcase }
wants_json = normalized.fetch("accept").include?("json")
host = normalized.fetch("host")

puts "host=#{host}"
puts "accept=#{accept}"
puts "wants_json=#{wants_json}"
puts "header_count=#{headers.length}"
accept = "text/html"
headers = {
  "Host" => "example.com",
  "Accept" => accept,
  "User-Agent" => "egtry-demo"
}

normalized = headers.transform_keys { |key| key.downcase }
wants_json = normalized.fetch("accept").include?("json")
host = normalized.fetch("host")

puts "host=#{host}"
puts "accept=#{accept}"
puts "wants_json=#{wants_json}"
puts "header_count=#{headers.length}"
accept = "application/xml"
headers = {
  "Host" => "example.com",
  "Accept" => accept,
  "User-Agent" => "egtry-demo"
}

normalized = headers.transform_keys { |key| key.downcase }
wants_json = normalized.fetch("accept").include?("json")
host = normalized.fetch("host")

puts "host=#{host}"
puts "accept=#{accept}"
puts "wants_json=#{wants_json}"
puts "header_count=#{headers.length}"
  1. accept ← application/json, headers ← {"Host"=>"example.com", "Accept"=>"application/json", "User-Agent"=>"egtry-demo"}

    1accept→ application/json = "application/json"  #@accept="text/html", "application/xml"2headers→ {"Host"=>"example.com", "Accept"=>"application/json", "User-Agent"=>"egtry-demo"} = {3  "Host" => "example.com",4  "Accept" => acceptapplication/json,5  "User-Agent" => "egtry-demo"6}78normalized→ {"host"=>"example.com", "accept"=>"application/json", "user-agent"=>"egtry-demo"} = headers{"Host"=>"example.com", "Accept"=>"application/json", "User-Agent"=>"egtry-demo"}.transform_keys { |key| key.downcase }9wants_json→ true = normalized.fetch("accept").include?("json")true10host→ example.com = normalized.fetch("host")example.com1112puts "host=#{hostexample.com}"13puts "accept=#{acceptapplication/json}"14puts "wants_json=#{wants_jsontrue}"15puts "header_count=#{headers.length3}"
    outputhost=example.com
    accept=application/json
    wants_json=true
    header_count=3
  1. accept ← text/html, headers ← {"Host"=>"example.com", "Accept"=>"text/html", "User-Agent"=>"egtry-demo"}

    1accept→ text/html = "text/html"2headers→ {"Host"=>"example.com", "Accept"=>"text/html", "User-Agent"=>"egtry-demo"} = {3  "Host" => "example.com",4  "Accept" => accepttext/html,5  "User-Agent" => "egtry-demo"6}78normalized→ {"host"=>"example.com", "accept"=>"text/html", "user-agent"=>"egtry-demo"} = headers{"Host"=>"example.com", "Accept"=>"text/html", "User-Agent"=>"egtry-demo"}.transform_keys { |key| key.downcase }9wants_json→ false = normalized.fetch("accept").include?("json")false10host→ example.com = normalized.fetch("host")example.com1112puts "host=#{hostexample.com}"13puts "accept=#{accepttext/html}"14puts "wants_json=#{wants_jsonfalse}"15puts "header_count=#{headers.length3}"
    outputhost=example.com
    accept=text/html
    wants_json=false
    header_count=3
  1. accept ← application/xml, headers ← {"Host"=>"example.com", "Accept"=>"application/xml", "User-Agent"=>"egtry-demo"}

    1accept→ application/xml = "application/xml"2headers→ {"Host"=>"example.com", "Accept"=>"application/xml", "User-Agent"=>"egtry-demo"} = {3  "Host" => "example.com",4  "Accept" => acceptapplication/xml,5  "User-Agent" => "egtry-demo"6}78normalized→ {"host"=>"example.com", "accept"=>"application/xml", "user-agent"=>"egtry-demo"} = headers{"Host"=>"example.com", "Accept"=>"application/xml", "User-Agent"=>"egtry-demo"}.transform_keys { |key| key.downcase }9wants_json→ false = normalized.fetch("accept").include?("json")false10host→ example.com = normalized.fetch("host")example.com1112puts "host=#{hostexample.com}"13puts "accept=#{acceptapplication/xml}"14puts "wants_json=#{wants_jsonfalse}"15puts "header_count=#{headers.length3}"
    outputhost=example.com
    accept=application/xml
    wants_json=false
    header_count=3