1

So far this is what I've come up with.

RestClient.proxy = ENV("http_proxy")
RestClient.head("Cookie","accessToken=#{@GottenTokenString}") #this line not working
resp = RestClient.get("http://someServer/path/RestAPIPage.json",{:params => {:count => @count, :userCount => @userCount}})

The problem I have here is that I can't seem to add any headers to the get request. The API I am calling requires that a token be obtained and set to a cookie header. Seems that the REST API can't handle this.

The reason I've decided to use this the RESTCLIENT gem is because the NET::HTTP.get does not allow the usage of methods that allow adding a query. Summarizing, I can't add headers with RESTCLIENT. But I can't add queries with NET::HTTP.

I'm really stumped.

Michael Lupo
  • 326
  • 3
  • 17
  • See [this question](http://stackoverflow.com/questions/12161640/setting-request-headers-in-ruby) and [its answer](http://stackoverflow.com/a/12161762/351716). – Teemu Leisti Jun 28 '13 at 09:30

2 Answers2

1

Old question, but adding my .02 for reference since I just struggled through the same...

In my case, I wanted to pass a token in the header of a GET request, and also add params. The trick was to include the header like a param, but not include it in the params hash. "Authorization" => "Bearer #{token}" is the header info in my example.

require 'json'
require 'rest-client'

JSON.load(RestClient.get("http://yourUrlGoesHere.com", {"Authorization" => "Bearer #{token}", :params => {:foo => 'bar'}}))
patkil
  • 1,929
  • 3
  • 16
  • 17
  • it doesnt' run for me. I can't insert any header parameters. it seem always rest-client (1.7.2) seems considers all parmeters as "params" :-( – Giorgio Robino Nov 28 '14 at 10:46
1

In a similar situation I went with:

session = RestClient::Resource.new "url"
response = session.post("", :Cookie => @cookie_variable)

Or for get it was straight:

RestClient.get(url, :Cookie => @cookie_variable)

I never did get the head syntax to work.

ScottJShea
  • 7,041
  • 11
  • 44
  • 67