使用 RestClient 传入 API 键和参数
Passing in API Key and Parameters with RestClient
在我的应用程序中,我让用户传入一个表单,该表单会触发 API 请求,然后显示查询结果。
我允许用户select设置很多或几个参数。我遇到的问题是 401 授权错误,我认为这是因为 api 密钥未被识别(没有密码,api 需要用户名并且没有限制)。
申请:
post '/search' do
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
分隔符是我用来捕获所有传入参数的分隔符,因此我可以向用户显示他们搜索的内容。我已经阅读了 https://github.com/rest-client/rest-client 上的文档,他们没有提到传入 api 键。
这是重构过程的一部分 - 一个一个地传递参数作为 #{@parameter_example} 有效但使我的代码可读性降低,然后我必须手动设置 @parameter_example = "¶meter_example=#{parameter_example}" 这似乎过于冗长。
从 capitalwords.org documentation, it seems that the api key
along with the phrase
, start_date
, end_date
params should be passed as part of the query string. 判断,所以你的 rest-client 请求应该是这样的:
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)
我认为,为了将这样的参数(使用 headers 参数哈希)传递给 RestClient::Requeest.execute
,那么您请求的 url 不应包含任何参数,否则 rest-client 无法生成正确的 url。这就是为什么我将 page
和 phrase
从 url 移动到参数短语哈希中。
您是否记得在文件顶部执行以下操作?
require 'dotenv'
Dotenv.load
在我的应用程序中,我让用户传入一个表单,该表单会触发 API 请求,然后显示查询结果。
我允许用户select设置很多或几个参数。我遇到的问题是 401 授权错误,我认为这是因为 api 密钥未被识别(没有密码,api 需要用户名并且没有限制)。
申请:
post '/search' do
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
分隔符是我用来捕获所有传入参数的分隔符,因此我可以向用户显示他们搜索的内容。我已经阅读了 https://github.com/rest-client/rest-client 上的文档,他们没有提到传入 api 键。
这是重构过程的一部分 - 一个一个地传递参数作为 #{@parameter_example} 有效但使我的代码可读性降低,然后我必须手动设置 @parameter_example = "¶meter_example=#{parameter_example}" 这似乎过于冗长。
从 capitalwords.org documentation, it seems that the api key
along with the phrase
, start_date
, end_date
params should be passed as part of the query string. 判断,所以你的 rest-client 请求应该是这样的:
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)
我认为,为了将这样的参数(使用 headers 参数哈希)传递给 RestClient::Requeest.execute
,那么您请求的 url 不应包含任何参数,否则 rest-client 无法生成正确的 url。这就是为什么我将 page
和 phrase
从 url 移动到参数短语哈希中。
您是否记得在文件顶部执行以下操作?
require 'dotenv'
Dotenv.load