Ruby 中的参数化获取请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1252210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parametrized get request in Ruby?
提问by Tom Lehman
How do I make an HTTP GETrequest with parameters in Ruby?
如何GET在 Ruby 中使用参数发出 HTTP请求?
It's easy to do when you're POSTing:
当你在POSTing时很容易做到:
require 'net/http'
require 'uri'
HTTP.post_form URI.parse('http://www.example.com/search.cgi'),
{ "q" => "ruby", "max" => "50" }
But I see no way of passing GETparameters as a hash using 'net/http'.
但是我看不到GET使用'net/http'.
采纳答案by Chris Moos
Use the following method:
使用以下方法:
require 'net/http'
require 'cgi'
def http_get(domain,path,params)
return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))) if not params.nil?
return Net::HTTP.get(domain, path)
end
params = {:q => "ruby", :max => 50}
print http_get("www.example.com", "/search.cgi", params)
回答by lime
Since version 1.9.2 (I think) you can actually pass the parameters as a hash to the URI::encode_www_formmethod like this:
从 1.9.2 版(我认为)开始,您实际上可以将参数作为散列传递给URI::encode_www_form方法,如下所示:
require 'uri'
uri = URI.parse('http://www.example.com/search.cgi')
params = { :q => "ruby", :max => "50" }
# Add params to URI
uri.query = URI.encode_www_form( params )
and then fetch the result, depending on your preference
然后根据您的喜好获取结果
require 'open-uri'
puts uri.open.read
or
或者
require 'net/http'
puts Net::HTTP.get(uri)
回答by marc
require 'net/http' require 'uri'
uri = URI.parse( "http://www.google.de/search" ); params = {'q'=>'cheese'}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.path)
request.set_form_data( params )
# instantiate a new Request object
request = Net::HTTP::Get.new( uri.path+ '?' + request.body )
response = http.request(request)
puts response.body
I would expect it to work without the second instantiation as it would be the first request-objects or the http-object's job but it worked for me this way.
我希望它可以在没有第二个实例化的情况下工作,因为它将是第一个请求对象或 http 对象的工作,但它以这种方式对我有用。
回答by mike
Net::HTTP.get_print 'localhost', '/cgi-bin/badstore.cgi?searchquery=crystal&action=search&x=11&y=15'
or
或者
uri = URI.parse("http://localhost")
req = Net::HTTP::Get.new("/cgi-bin/badstore.cgi?searchquery=crystal&action=search&x=11&y=15")
http = Net::HTTP.new(uri.host, uri.port)
response = http.start do |http|
http.request(req)
end
回答by Alex Dean
回答by Michael Glass
new to stack overflow and I guess I can't comment, but @chris.moose is missing double quotes in his function def. line 5 should be:
堆栈溢出的新手,我想我无法发表评论,但@chris.moose 在他的函数 def 中缺少双引号。第 5 行应该是:
return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&'))) if not params.nil?
or here's the whole thing redefined for copy/pasting
或者这是为复制/粘贴重新定义的整个事情
require 'net/http'
require 'cgi'
def http_get(domain,path,params)
return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&'))) if not params.nil?
return Net::HTTP.get(domain, path)
end
<3 -mike
<3-迈克
回答by user2903934
This is the easiest way to do it
require 'net/http' require 'uri'
@siteurl = "http://www.google.de/search/#{@yourquery}"
define your query
@yourquery = "whatever"
uri = URI.parse( "@siteurl" );
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.path)
# instantiate a new Request object
request = Net::HTTP::Get.new( uri.path+ '?' + request.body )
response = http.request(request)
puts response.body
Might not be perfect but at least you get the idea.
可能不完美,但至少你明白了。

