Need help passing variable to Net::HTTP

  • Thread starter Samuel Sternhagen
  • Start date
S

Samuel Sternhagen

I have used Net::HTTP like this:

Net::HTTP.get 'google.com', '/index.html'

I would like to pass it a variable instead.

I tried this:

irb(main):001:0> require 'net/http'
=> true
irb(main):002:0> url = String.new
=> ""
irb(main):003:0> url = "'google.com', '/index.html'"
=> "'google.com', '/index.html'"
irb(main):004:0> Net::HTTP.get_print(url)
NoMethodError: undefined method `host' for "'google.com',
'/index.html'":String
from /usr/lib/ruby/1.8/net/http.rb:379:in `get_response'
from /usr/lib/ruby/1.8/net/http.rb:337:in `get_print'
from (irb):4
from :0

How do I pass a variable to Net:HTTP ?
 
J

Jesús Gabriel y Galán

I have used Net::HTTP like this:

Net::HTTP.get 'google.com', '/index.html'

I would like to pass it a variable instead.

You already got your answer, but I wanted to point out that:
irb(main):001:0> require 'net/http'
=> true
irb(main):002:0> url = String.new
=> ""

You don't need to do this, since you are assigning a new value to the
url variable just below. You are creating an object that you don't use
at all.
irb(main):003:0> url = "'google.com', '/index.html'"
=> "'google.com', '/index.html'"

Jesus.
 
S

Samuel Sternhagen

tilde said:
url="google.com/index.html"
Net::HTTP.get_print(URI.parse(url))

get and get_print expect an URI, if you don't specify both host and path
as strings.

Thanks :D It is working now.
This one had me stuck for hours.
 
S

Samuel Sternhagen

Jesús Gabriel y Galán said:
You already got your answer, but I wanted to point out that:


You don't need to do this, since you are assigning a new value to the
url variable just below. You are creating an object that you don't use
at all.


Jesus.

Thanks for the tip Jesus. I used to program in C so I have some bad
habits. I just tried my code without creating all those objects and
everything went fine.
 
V

Vassilis Rizopoulos

Thanks for the tip Jesus. I used to program in C so I have some bad
habits. I just tried my code without creating all those objects and
everything went fine.
Might I suggest reading the first few chapters of the original Pickaxe
(http://ruby-doc.org/docs/ProgrammingRuby/) - lets say up to Basic I/O.
Although it was written for 1.6 and is woefully out-of-date as a Ruby
reference, I always found it gave one of the more digestible
explanations of OO and dynamic typing.
Ofcourse you can go right ahead and buy the newest version.
Cheers,
V.-
 
B

Brian Candler

Samuel said:
I have used Net::HTTP like this:

Net::HTTP.get 'google.com', '/index.html'

I would like to pass it a variable instead.

I tried this:

irb(main):001:0> require 'net/http'
=> true
irb(main):002:0> url = String.new
=> ""
irb(main):003:0> url = "'google.com', '/index.html'"
=> "'google.com', '/index.html'"

Here's another option you could use:

url = ['google.com', '/index.html']
Net::HTTP.get_print(*url)

or more simply:

host = 'google.com'
path = '/index.html'
Net::HTTP.get_print(host, path)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top