Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Request Headers in Ruby

Tags:

rest

ruby

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

url = 'http://someurl' request =  {"data" => data}.to_json response = RestClient.post(url,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.

like image 540
Charlie Davies Avatar asked Aug 28 '12 14:08

Charlie Davies


2 Answers

The third parameter is the headers hash.

You can do what you want by:

response = RestClient.post(    url,    request,   :content_type => :json, :accept => :json, :'x-auth-key' => "mykey") 
like image 185
Maurício Linhares Avatar answered Sep 28 '22 00:09

Maurício Linhares


You can also do this

RestClient::Request.execute(    :method => :get or :post,    :url => your_url,    :headers => {key => value} ) 
like image 22
rrrrong Avatar answered Sep 28 '22 00:09

rrrrong