Search code examples
ruby-on-railsrest-client

Accessing header params in RestClient API get call


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.


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"