Reputation: 4483
I am using RestClient in my ruby on rails app to send a get request to an api of my app like the following:
RestClient.get(url?params1=<val1>¶m2=<val2>..)
The problem is that I also have to pass the secret API key to access the api in header of the get request. I have no idea how to pass this parameter in header of the request and how to access this param api_key inside my controller to validate the request to get access to that api. So I want something like that:
class StudentsController < ApplicationController
before_filter :validate_access, :only => [:<api_name>]
private
def validate_access
.... < I don't have any idea what to write here>
In this restrict_access function I want to check the value of api_key in request header to determine whether the request will be given access to that function or not.
Upvotes: 2
Views: 6224
Reputation: 231
You can set header values to RestClient.get
by 2nd argument.
RestClient.get "/url?params1=<val1>¶m2=<val2>..", {x_your_api_token: "YOUR_API_TOKEN"}
And in ActionController, you can get it by request.headers["X-Your-API-Token"]
def validate_access
api_token = request.headers["X-Your-API-Token"] # header name is case insentsitive
...
end
See:
Upvotes: 3