gpt4 book ai didi

ruby-on-rails - 有什么方法可以使用 Hydra 和 Typhoeus 将请求与响应相匹配吗?

转载 作者:数据小太阳 更新时间:2023-10-29 07:30:06 27 4
gpt4 key购买 nike

有什么方法可以将请求 URL 与该 URL 的响应正文相匹配吗?

我将 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

最佳答案

一切都在那里,但您需要深入研究 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/"

如果我没记错的话,第一个是你要求的,第二个是你在处理重定向后得到的,即你真正登陆的地方。

关于ruby-on-rails - 有什么方法可以使用 Hydra 和 Typhoeus 将请求与响应相匹配吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27710166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com