Question

I'm trying to send a GET to http://www.hello.com/sup?a=b in ruby 1.9.3-p194 (can't update the version due to legacy code)

uri = URI.parse("http://www.hello.com/sup?a=b")
uri.query = "a=b"

req = Net::HTTP::Get.new(uri)
response = Net::HTTP.start(uri.host, uri.port) { |http| http.request(req) }
case response
  when Net::HTTPSuccess     then response
  else
     puts "Error"
end

I'm actually using ruby 1.9.3-p194 but I'm getting this error:

/Users/hithere/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1860:in `initialize': undefined method `empty?' for #<URI::HTTP:0x007f938d9051c8> (NoMethodError)
from /Users/hithere/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:2093:in `initialize'
from send_to_hg_given_place_id.rb:101:in `new'
from send_to_hg_given_place_id.rb:101:in `block in fetch'
from /Users/hithere/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
from send_to_hg_given_place_id.rb:100:in `fetch'
from send_to_hg_given_place_id.rb:141:in `block in <main>'
from send_to_hg_given_place_id.rb:133:in `each'
from send_to_hg_given_place_id.rb:133:in `<main>'

For some reason it is trying to use http.rb 1.9.1, and 1.9.1 requires the parameter in #new to be a String instead of URI. I'd like to either fix it so it uses 1.9.3, or obtain a solution that works for 1.9.1 http.rb

Was it helpful?

Solution

You can refer to examples for Net::HTTP. You need to pass a string in Net::HTTP::Get.new

Here is an example from it (note uri.request_uri):

uri = URI('http://example.com/some_path?query=string')

Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri.request_uri

  response = http.request request # Net::HTTPResponse object
end

GET parameters you can append to the URL. Just pay attention to the URL encoding. See for example this SO question.

OTHER TIPS

instead of

uri = URI.parse(http://www.hello.com/sup?a=b)

it would be

uri = URI.parse("http://www.hello.com/sup?a=b")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top