2014-12-30 4 views
2

요청 URL을 해당 URL의 응답 본문과 일치시키는 방법이 있습니까?요청을 Hydra와 Typhoeus를 사용하여 응답 할 수있는 방법이 있습니까?

나는 Hydra를 GET 요청에 사용하고 있으며 항목 ID는 URL에 표시되지만 응답 본문에는 나타나지 않고 항목에 대한 세부 정보 만 표시됩니다. 100 개의 HTTP 요청은 정상적으로 전송되지만 응답을 정렬 할 수는 없습니다.

대안으로 OpenUri를 사용하고 한 번에 하나씩 요청을 보냅니다.

products.each do |list| 
url = www.example.com/list.id 
hydra.queue(request) 
request.on_complete do |response| 
    if response.body["price"] != list_all.find(request.id?).price 
    puts "I can't tell which response goes to which request" 
    end 
end 
end 

솔루션

response.effective_url 

답변

0

당신은 여전히 ​​list 변수에 액세스 할 수 있습니다, 그래서 당신은 그냥 거기에서 얻을 수 있을까? Like :

products.each do |list| 
    url = www.example.com/list.id 
    hydra.queue(request) 
    request.on_complete do |response| 
     puts "#{list.id}, #{response.body}" 
    end 
    end 
3

모든 것이 있지만 Hydra가받은 응답을 파헤쳐 야합니다. 예를 들어

: 여기

require 'typhoeus' 

request = Typhoeus::Request.new(
    'www.example.net', 
    method: :get, 
    headers: { Accept: 'text/html' } 
) 

hydra = Typhoeus::Hydra.hydra 
hydra.queue(request) 
hydra.run 

는 응답이고 원래 요청입니다 내부 :

response = request.response 
response.request 
# => #<Typhoeus::Request:0x007f9093f9f288 
#  @base_url="www.example.net", 
#  @hydra= 
#  #<Typhoeus::Hydra:0x007f9093f9f0d0 
#  @max_concurrency=200, 
#  @memory={}, 
#  @multi= 
#  #<Ethon::Multi:0x007f9093f9f030 
#   @easy_handles=[], 
#   @fd_excep=#<Ethon::Curl::FDSet:0x007f9093f9eba8>, 
#   @fd_read=#<Ethon::Curl::FDSet:0x007f9093f9ed60>, 
#   @fd_write=#<Ethon::Curl::FDSet:0x007f9093f9ec48>, 
#   @handle=#<FFI::AutoPointer address=0x007f9095c74740>, 
#   @max_fd=#<FFI::MemoryPointer address=0x007f9094f484d0 size=4>, 
#   @running_count=0, 
#   @timeout=#<FFI::MemoryPointer address=0x007f9094f3a7e0 size=8>, # !> instance variable @on_body not initialized 
#   @timeval=#<Ethon::Curl::Timeval:0x007f9093f9ee78>>, 
#  @options={}, 
#  @queued_requests=[]>, 
#  @on_complete=[], 
#  @on_headers=[], 
#  @on_success=[], 
#  @options= 
#  {:method=>:get, 
#  :headers=> 
#  {"User-Agent"=>"Typhoeus - https://github.com/typhoeus/typhoeus", 
#   :Accept=>"text/html"}, 
#  :maxredirs=>50}, 
#  @original_options={:method=>:get, :headers=>{:Accept=>"text/html"}}, 
#  @response= 
#  #<Typhoeus::Response:0x007f9093f97d08 
#  @options= 
#  {:httpauth_avail=>0, 
#   :total_time=>0.11088, 
#   :starttransfer_time=>0.110587, 
#   :appconnect_time=>0.0, 
#   :pretransfer_time=>0.053747, 
#   :connect_time=>0.053611, 
#   :namelookup_time=>0.001304, 
#   :effective_url=>"HTTP://www.example.net/", 
#   :primary_ip=>"93.184.216.34", 
#   :response_code=>200, 
#   :request_size=>121, 
#   :redirect_count=>0, 
#   :return_code=>:ok, 
#   :response_headers=> 
#   "HTTP/1.1 200 OK\r\nAccept-Ranges: bytes\r\nCache-Control: max-age=604800\r\nContent-Type: text/html\r\nDate: Tue, 30 Dec 2014 19:24:10 GMT\r\nEtag: \"359670651\"\r\nExpires: Tue, 06 Jan 2015 19:24:10 GMT\r\nLast-Modified: Fri, 09 Aug 2013 23:54:35 GMT\r\nServer: ECS (cpm/F9FC)\r\nX-Cache: HIT\r\nx-ec-custom-error: 1\r\nContent-Length: 1270\r\n\r\n", 
#   :response_body=> 
#   "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n  background-color: #f0f0f2;\n  margin: 0;\n  padding: 0;\n  font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n  \n }\n div {\n  width: 600px;\n  margin: 5em auto;\n  padding: 50px;\n  background-color: #fff;\n  border-radius: 1em;\n }\n a:link, a:visited {\n  color: #38488f;\n  text-decoration: none;\n }\n @media (max-width: 700px) {\n  body {\n   background-color: #fff;\n  }\n  div {\n   width: auto;\n   margin: 0 auto;\n   border-radius: 0;\n   padding: 1em;\n  }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n", 
#   :debug_info=> 
#   #<Ethon::Easy::DebugInfo:0x007f9093f9de60 @messages=[]>}, 
#  @request=#<Typhoeus::Request:0x007f9093f9f288 ...>>> 

내부가 요청의 base_url :

response.request.base_url 
# => "www.example.net" 

그리고 effective_url :

내가 기억한다면 첫 번째 질문은 귀하가 요청한 것이며 두 번째는 리디렉션 처리 후, 즉 실제로 착륙 한 곳입니다.

+0

고마워요! 그건 내가 찾고 있었던 100 % 였어. –

관련 문제