Question

I am doing a RestClient api call to one of my controller as follows:

RestClient.get(url,{:secret_key => "abcd"})

But when I am printing the params in my controller as follows:

p params

I cannot find the secret_key in the controller. From https://github.com/rest-client/rest-client/blob/master/lib/restclient.rb#L71 I learnt that header params are passed in RestClient API get call like the above.

But I don't know how to access the header. So if anyone helps me in figuring this out I will be really grateful.

Was it helpful?

Solution

If you want get secret_key in params hash you can pass this parameter through url string like:

url = "http://your.api?secret_key=secret"
RestClient.get(url)

or if you want pass secret_key though headers you should:

RestClient.get(url, {"secret_key" => "secret"})

in rails controller:

secret_key = request.headers["secret_key"]
=> "secret"

OTHER TIPS

You are setting the header and checking for params..

So, first parameter is params to the request, second is headers

RestClient.get url, {:secret_key => "abcd"}, {:user_agent => user_agent}

If you only want to send params do

RestClient.get url, :params => {:secret_key => "abcd"}

If you want to check the response header (RestClient.get(url,{:secret_key => "abcd"})) do

response['secrect_key']

You can pass http params via url in this way:

RestClient.get("http://your.api?secret_key=secret")

For my version of rest-client (1.6.7), the signature for RestClient.get specifies HTTP HEADER parameters in the 2nd argument:

  def self.get(url, headers={}, &block)
    Request.execute(:method => :get, :url => url, :headers => headers, &block)
  end

What I recall for the RestClient.post is that the HTTP HEADER parameters can be specified without braces:

RestClient.post(url, :content_type => "application/pdf", :accept => "application/json")

I would guess RestClient.get works in a similar fashion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top