2016-05-24 51 views
1

我提出一個要求,並出現以下Ruby腳本的響應:的Net :: HTTP ::獲取到RestClient.get

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中進行遊戲。我如何構建選項散列?

+1

這意味着你被拒絕,因爲可能有某種類型的訪問控制機制阻止你。您的請求沒有任何不妥之處。 – tadman

回答

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