30

I'm making an HTTP request with Ruby using Net::HTTP, and I can't figure out how to get all the response headers.

I tried response.header and response.headers and nothing is working.

6
  • Net/HTTP has a notoriously bad API. If you were using another one, such as httpclient, response.header would have just worked. Feb 6, 2013 at 1:00
  • What do you mean by bad? Why do I want to avoid it? Feb 6, 2013 at 1:09
  • The library itself isn't bad, but the API is clunky and unintuitive as you have discovered. I always use httpclient or a wrapper library with more features like HTTParty, Rest-Client, etc. when I can. Feb 6, 2013 at 1:40
  • Thanks for the feedback! Definitely something to consider for the future! Thanks again. Feb 6, 2013 at 1:48
  • 4
    Net::HTTP is a fairly low-level library, so you have to write a bit more code to accomplish the same thing you can do with other libraries, however, at the same time, it also gives you access to things you can't do in other libraries. Its API isn't bad, nor is it clunky, it's just low level. Feb 6, 2013 at 2:53

6 Answers 6

62

The response object actually contains the headers.

See "Net::HTTPResponse" for more infomation.

You can do:

response['Cache-Control']

You can also call each_header or each on the response object to iterate through the headers.

If you really want the headers outside of the response object, call response.to_hash

18

The response Net::HTTPResponse contains headers from Net::HTTPHeader which you can get from each_header method as said by @Intrepidd which will return an enumerator as below:

response.each_header

#<Enumerator: #<Net::HTTPOK 200 OK readbody=true>:each_header>
[
  ["x-frame-options", "SAMEORIGIN"],
  ["x-xss-protection", "1; mode=block"],
  ["x-content-type-options", "nosniff"],
  ["content-type", "application/json; charset=utf-8"],
  ["etag", "W/\"51a4b917285f7e77dcc1a68693fcee95\""],
  ["cache-control", "max-age=0, private, must-revalidate"],
  ["x-request-id", "59943e47-5828-457d-a6da-dbac37a20729"],
  ["x-runtime", "0.162359"],
  ["connection", "close"],
  ["transfer-encoding", "chunked"]
]

You can get the actual hash using to_h method as below:

response.each_header.to_h

{
  "x-frame-options"=>"SAMEORIGIN", 
  "x-xss-protection"=>"1; mode=block", 
  "x-content-type-options"=>"nosniff", 
  "content-type"=>"application/json; charset=utf-8", 
  "etag"=>"W/\"51a4b917285f7e77dcc1a68693fcee95\"", 
  "cache-control"=>"max-age=0, private, must-revalidate", 
  "x-request-id"=>"59943e47-5828-457d-a6da-dbac37a20729", 
  "x-runtime"=>"0.162359", 
  "connection"=>"close", 
  "transfer-encoding"=>"chunked"
}
4

Note that the RestClient library has the expected behaviour for response.headers.

response.headers
{
                          :server => "nginx/1.4.7",
                            :date => "Sat, 08 Nov 2014 19:44:58 GMT",
                    :content_type => "application/json",
                  :content_length => "303",
                      :connection => "keep-alive",
             :content_disposition => "inline",
     :access_control_allow_origin => "*",
          :access_control_max_age => "600",
    :access_control_allow_methods => "GET, POST, PUT, DELETE, OPTIONS",
    :access_control_allow_headers => "Content-Type, x-requested-with"
}
4

If you need user friendly output then each_capitalized can be used:

response.each_capitalized { |key, value| puts " - #{key}: #{value}" }

This will print:

 - Content-Type: application/json; charset=utf-8
 - Transfer-Encoding: chunked
 - Connection: keep-alive
 - Status: 401 Unauthorized
 - Cache-Control: no-cache
 - Date: Wed, 28 Nov 2018 09:06:39 GMT
1
  • This should be the preferred answer. Nov 28, 2018 at 16:54
1

This is also easily achieved with HTTParty as an alternative:

HTTParty.get(uri).headers
-1

To store it in a hash =>

response_headers = {}
your_object.response.each { |key, value|  response_headers.merge!(key.to_s => value.to_s) }

puts response_headers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.