有没有办法将请求网址与该网址的响应正文相匹配?
我使用Hydra进行GET请求,并且项目ID显示在URL中,但不显示在响应正文中,只显示有关该项目的详细信息。 100个HTTP请求发送正常,但我无法对响应进行排序。
我的替代方案是使用OpenUri并一次发送1个请求。
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 :(得分:4)
一切都在那里,但你需要在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
:
response.effective_url
# => "HTTP://www.example.net/"
如果我没记错的话,第一个是你要求的,第二个是你处理重定向后得到的,即你真正落地的地方。
答案 1 :(得分:0)
你仍然可以访问list
变量,所以你不能从那里得到它吗?像:
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