Charlie Davies Charlie Davies - 2 years ago 111
Ruby Question

Setting Request Headers in Ruby

I have the rest client gem and I am defining a request like this:

url = 'http://someurl'
request = {"data" => data}.to_json
response =,request,:content_type => :json, :accept => :json)

However I need to set the HTTP header to something. For example an API key. Which could be done in curl as:

curl -XHEAD -H x-auth-user: myusername -H x-auth-key: mykey "url"

Whats the best way to do this in ruby? Using this gem? Or can I do it manually to have more control.

Answer Source

The third parameter is the headers hash.

You can do what you want by:

response = 
  :content_type => :json, :accept => :json, :'x-auth-key' => "mykey")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download