使用Ruby获取URL(使用params)
有人能告诉我如何使用Ruby获取(GET)URL(使用params)吗? 我在网上发现了一堆例子,但我找不到解释如何传递参数的例子。
require 'net/http' require 'uri' uri = URI.parse("http://www.example.com/?test=1") response = Net::HTTP.get_response uri p response.body
还有一些其他好的HTTP客户端或包装器,例如HTTParty。
require 'rubygems' require 'httparty' response = HTTParty.get("http://www.example.com/?test=1") p response.body
我使用类似下面的内容,它非常简单,不会让你构建自己的查询字符串:
require 'net/http' response = nil Net::HTTP.start "example.com", 80 do |http| request = Net::HTTP::Get.new "/endpoint" request.form_data = {:q => "123"} response = http.request(request) end
我错过了这个。 解决方案就在这里。
Ruby中的参数化get请求?
与你如何通过任何语言获得params相同。
http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=how+to+pass+get+params+with+ruby
以上将有以下参数哈希。
{:sourceid => chrome, :ie => UTF-8, :q => "how+to+pass+get+params+with+ruby"}