我发出请求并使用以下ruby脚本获得响应:
require 'uri'
require 'net/http'
require 'openssl'
url = URI("https://somesite.com/api/advisors/2050212?api_key=some_key&username=some_username)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(url)
request["cache-control"] = 'no-cache'
request["token"] = 'token'
response = http.request(request)
puts response.read_body
使用RestClient我构建以下内容:
resource = RestClient::Resource.new 'https://somesite.com/api/advisors', {params: {id: 2050212, api_key: 'some_key', username: 'some_username' }}
resource.get
我收到了这个回复:
RestClient::Unauthorized: 401 Unauthorized
from /Users/user/.rvm/gems/ruby-2.3.1/gems/rest-client-1.8.0/lib/restclient/abstract_response.rb:74:in `return!'
第一次涉猎RestClient。如何构建选项哈希?
答案 0 :(得分:0)
正如@tadman所说,你设置了所有选项。
您收到的是401,因为您将header["token"] = 'token'
设置为Net::HTTP
,但未使用RestClient
我认为这种方法会有所帮助。
resource = RestClient::Resource.new 'https://somesite.com/api/advisors', {params: {id: 2050212, api_key: 'some_key', username: 'some_username' }}
resource.get({'token' => 'token'})
来自here