# Need help passing variable to Net::HTTP

**URL:** <https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952>\
**Category:** Ruby\
**Created:** [August 9, 2010, 4:18am UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952 "2010-08-09T04:18:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 4:18am UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/1 "2010-08-09T04:18:35Z")

</div>

I have used Net::HTTP like this:

Net::HTTP.get ‘[google.com](http://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](http://google.com)’, ‘/index.html’”  
=\> “‘[google.com](http://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 ?

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 7:35am UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/2 "2010-08-09T07:35:27Z")

</div>

On 08/09/10 04:18, Samuel S. wrote:

> url = “‘[google.com](http://google.com)’, ‘/index.html’”

url=“[Google](http://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.

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 8:28am UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/3 "2010-08-09T08:28:43Z")

</div>

On Mon, Aug 9, 2010 at 4:18 AM, Samuel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have used Net::HTTP like this:
> 
> Net::HTTP.get ‘[google.com](http://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](http://google.com)’, ‘/index.html’”  
> =\> “‘[google.com](http://google.com)’, ‘/index.html’”

Jesus.

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 8:46am UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/4 "2010-08-09T08:46:53Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Mon, Aug 9, 2010 at 4:18 AM, Samuel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I have used Net::HTTP like this:
> > 
> > Net::HTTP.get ‘[google.com](http://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](http://google.com)’, ‘/index.html’”  
> > =\> “‘[google.com](http://google.com)’, ‘/index.html’”
> 
> 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.

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 8:45am UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/5 "2010-08-09T08:45:14Z")

</div>

tilde wrote:

> On 08/09/10 04:18, Samuel S. wrote:
> 
> > url = “‘[google.com](http://google.com)’, ‘/index.html’”
> 
> url=“[Google](http://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 😃 It is working now.  
This one had me stuck for hours.

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 10:54am UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/6 "2010-08-09T10:54:59Z")

</div>

Samuel S. wrote:

> I have used Net::HTTP like this:
> 
> Net::HTTP.get ‘[google.com](http://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](http://google.com)’, ‘/index.html’”  
> =\> “‘[google.com](http://google.com)’, ‘/index.html’”

Here’s another option you could use:

url = [‘[google.com](http://google.com)’, ‘/index.html’]  
Net::HTTP.get\_print(\*url)

or more simply:

host = ‘[google.com](http://google.com)’  
path = ‘/index.html’  
Net::HTTP.get\_print(host, path)

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 9:53am UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/7 "2010-08-09T09:53:01Z")

</div>

On 09/08/10 09:46 , Samuel S. wrote:

> 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  
> ([Programming Ruby: The Pragmatic Programmer's Guide](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.-

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/8 "2010-08-09T18:25:56Z")

</div>

On 08/09/10 08:45, Samuel S. wrote:

> Thanks 😃 It is working now.  
> This one had me stuck for hours.

No problem ^^

---

<div class="post-metadata">

**Author:** ![shortnose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@shortnose](https://www.ruby-forum.com/u/shortnose)\
**Post date:** [August 9, 2010, 1:14pm UTC](https://www.ruby-forum.com/t/need-help-passing-variable-to-net-http/192952/9 "2010-08-09T13:14:27Z")

</div>

Samuel S. wrote:

> I have used Net::HTTP like this:

Hi Sam try to use Nokogiri gem  
it will be easy (to unterstand) and fast also
