Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get response headers from Ruby HTTP request

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.

like image 567
BlackHatSamurai Avatar asked Feb 06 '13 00:02

BlackHatSamurai


5 Answers

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

like image 70
Intrepidd Avatar answered Sep 28 '22 18:09

Intrepidd


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"
}
like image 44
Gokul Avatar answered Sep 28 '22 19:09

Gokul


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"
}
like image 22
Dennis Avatar answered Sep 28 '22 19:09

Dennis


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
like image 31
Vlad Avatar answered Sep 28 '22 19:09

Vlad


This is also easily achieved with HTTParty as an alternative:

HTTParty.get(uri).headers
like image 22
Jeremy Lynch Avatar answered Sep 28 '22 19:09

Jeremy Lynch