Dave Dave - 2 months ago 12
Ruby Question

How do I figure out what all the request headers in my request object are?

I’m using Rails 4.2.7. I have a request I set up like so:

2.3.0 :001 > url = 'http://www.mydomeina.com/results/browse.cfm?ID=4187141102&Gen=B&Begin=1&End=31&Max=31'
=> "http://www.mydomeina.com/results/browse.cfm?ID=4187141102&Gen=B&Begin=1&End=31&Max=31"
2.3.0 :002 > uri = URI(url)
=> #<URI::HTTP http://www.mydomeina.com/results/browse.cfm?ID=4187141102&Gen=B&Begin=1&End=31&Max=31>
2.3.0 :003 > req = Net::HTTP::Get.new uri
=> #<Net::HTTP::Get GET>
2.3.0 :004 > req["Referer"] = 'http://www.mydomeina.com/results/browse.cfm?ID=4187141102&Gen=B&Begin=1&End=31&Max=31'
=> "http://www.mydomeina.com/results/browse.cfm?ID=4187141102&Gen=B&Begin=1&End=31&Max=31"


I set
req["Referer"]
as an example of setting a header. There may be other headers in my request object. How do I figure out what they all are? This fails:

2.3.0 :009 > req.keys
NoMethodError: undefined method `keys' for #<Net::HTTP::Get GET>
Did you mean? key?


as does
req.headers
. How can I iterate over my request object to figure out what all the headers are? It is not an option to construct the request object in a different way then what I am doing now.

Answer

try

 req.each_key{|k| puts k}

or

 req.each{|k,v| puts "#{k} => #{v}"}

to see methods on an object from inside console you can always do

req.methods - Ojbect.methods