Show request headers


Often you'll be interested to see if you managed to set the headers as expected by an API. For that HTTPbin provides the /headers end-point.

First let's see what happens if we send a plain request to this end-point?


examples/net-http/get_request_headers_httpbin.rb
require 'net/http'

url = 'https://httpbin.org/headers'
uri = URI(url)
# puts(uri)

response = Net::HTTP.get_response(uri)
if response.is_a?(Net::HTTPSuccess)
    #puts "Headers:"
    #puts response.to_hash.inspect
    #puts '------'
    puts response.body
else
    puts response.code
end

This is the header as our Ruby code sent it. (Note the User Agent being Ruby)


{
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", 
    "Host": "httpbin.org", 
    "User-Agent": "Ruby", 
    "X-Amzn-Trace-Id": "Root=1-6381e195-0fefc04b79ad1bc14b4688b0"
  }
}