在Ruby中设置请求标头

我有其余的客户端gem,我正在定义这样的请求:

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

但是我需要将HTTP标头设置为某种东西。 例如API密钥。 这可以在curl中完成:

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

什么是在ruby中做到这一点的最好方法? 用这个gem? 或者我可以手动执行以获得更多控制权。

第三个参数是标题哈希 。

你可以做你想做的事:

 response = RestClient.post( url, request, :content_type => :json, :accept => :json, :'x-auth-key' => "mykey") 

你也可以这样做

 RestClient::Request.execute( :method => :get or :post, :url => your_url, :headers => {key => value} ) 

我有与Rest-Client(1.7.2)相同的问题我需要同时放置params和HTTP头。

我用这个语法解决了:

 params = {id: id, device: device, status: status} headers = {myheader: "giorgio"} RestClient.put url, params, headers 

我讨厌RestClient 🙂

如果不允许PUT我们可以在POST的标题中传递它。 标题以粗体显示。 这对我有用:

act_resp = RestClient.post url, req_param, **:content_type => :json, :method => :put* *