Networking Basics
Request Headers
HTTP headers are name/value pairs that describe a request.
Request Headers
request_headers.rb
accept =
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 =
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 =
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}"
headers
Normalizing header names makes lookup predictable even when callers use different capitalization.