ruby - Passing in API Key and Parameters with RestClient -


in application have user pass in form fires out api request , displays results of query.

i allow user to select many or few parameters. problem 401 authorization error , believe because apikey isn't being recognized (there no password, username needed api , no limits).

application:

post '/search'       phrase = params.fetch "phrase" #mandatory     @delimiters = ""      start_date = params.fetch "start_date"     start_date.empty? ? start_date = "" : @delimiters << "from #{start_date},"      end_date = params.fetch "end_date"      end_date.empty? ? end_date = "" : @delimiters << "to #{end_date}"      api_result = restclient::request.execute(method: :get, url: "capitolwords.org/api/1/text.json?phrase=#{phrase}                                          &page=0&apikey=",                                           headers: {params: {:start_date => start_date,                                           :end_date => end_date},                                          :authorization => env['sunlight_api_key']},                                           timeout: 10)        end 

the delimiter i'm using catch parameters passed in can show user searched by. i've read documentation @ https://github.com/rest-client/rest-client , don't mention passing in api keys.

this part of refactoring process - passing in parameters 1 one #{@parameter_example} works makes code less readable , manually must set @parameter_example = "&parameter_example=#{parameter_example}" seems overly verbose.

judging capitalwords.org documentation, seems api key along phrase, start_date, end_date params should passed part of query string. rest-client request should this:

api_result = restclient::request.execute(method: :get,                                              url: "capitolwords.org/api/1/text.json",                                          headers: {params: {:phrase => phrase,                                                             :start_date => start_date,                                                            :end_date => end_date,                                                            :page => 0,                                                            :apikey => env['sunlight_api_key']}},                                          timeout: 10)        

i think in order pass params (using headers params hash) restclient::requeest.execute url request should not include params or else rest-client fails produce correct url. that's why moved page , phrase url params phrase hash.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -