# Can't use variable from cgi with hpricot

**URL:** <https://www.ruby-forum.com/t/cant-use-variable-from-cgi-with-hpricot/177056>\
**Category:** Ruby\
**Created:** [October 10, 2009, 5:46pm UTC](https://www.ruby-forum.com/t/cant-use-variable-from-cgi-with-hpricot/177056 "2009-10-10T17:46:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wileydavis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b9bd4f/32.png) [@wileydavis](https://www.ruby-forum.com/u/wileydavis)\
**Post date:** [October 10, 2009, 5:46pm UTC](https://www.ruby-forum.com/t/cant-use-variable-from-cgi-with-hpricot/177056/1 "2009-10-10T17:46:20Z")

</div>

I’m trying to get a url from a form and return the page title of said  
url. I’m using cgi to get the web address from the url, which looks like  
this:

[http://somesite.com/result.rhtml?link=http%3A%2F%2Fpracticalpedal.com](http://somesite.com/result.rhtml?link=http%3A%2F%2Fpracticalpedal.com)

The result page embedded ruby looks like this:

\<%  
require ‘rubygems’  
require ‘cgi’  
require ‘open-uri’  
require ‘hpricot’

cgi = CGI.new

@linky = cgi[‘link’].to\_str

@result = open(@linky)  
@doc = Hpricot(@result)  
@title = (@doc/“title”).inner\_html  
@result.close

puts @title  
%\>

This works if I assign the @linky variable like so:

@linky = “[http://practicalpedal.com](http://practicalpedal.com)”

But not when getting it with cgi. I’m very new at all of this, as is  
probably obvious, but I can’t figure this out. Any pointers in the right  
direction would be appreciated.

---

<div class="post-metadata">

**Author:** ![wileydavis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b9bd4f/32.png) [@wileydavis](https://www.ruby-forum.com/u/wileydavis)\
**Post date:** [October 11, 2009, 4:05am UTC](https://www.ruby-forum.com/t/cant-use-variable-from-cgi-with-hpricot/177056/2 "2009-10-11T04:05:49Z")

</div>

Wiley Davis wrote:

> I’m trying to get a url from a form and return the page title of said  
> url. I’m using cgi to get the web address from the url, which looks like  
> this:
> 
> [http://somesite.com/result.rhtml?link=http%3A%2F%2Fpracticalpedal.com](http://somesite.com/result.rhtml?link=http%3A%2F%2Fpracticalpedal.com)
> 
> The result page embedded ruby looks like this:
> 
> \<%  
> require ‘rubygems’  
> require ‘cgi’  
> require ‘open-uri’  
> require ‘hpricot’
> 
> cgi = CGI.new
> 
> @linky = cgi[‘link’].to\_str

Disclaimer: I have never used embedded ruby before. I have used the CGI  
lib, however, so here’s my take.

cgi[‘link’] returns a string so there is no need for the TO\_STR

Other than that, your code looks ok to me, so I would check your form  
data. Something like the following should get recognized by cgi.

CGI is a fickle little LIB, but she’ll start to do your bidding once she  
feels that you know what you are doing. Pay particular attention to the  
 tag  
If you don’t specify NAME and use ID or some other thing, CGI wont read  
your code right. At least that has been my experience.

If you just can’t get CGI to read the form data, you can try to access  
the data using ARGV as if the string was a command line variable to your  
script.

I hope this helps you out. Good luck.

---

<div class="post-metadata">

**Author:** ![wileydavis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b9bd4f/32.png) [@wileydavis](https://www.ruby-forum.com/u/wileydavis)\
**Post date:** [October 11, 2009, 6:53am UTC](https://www.ruby-forum.com/t/cant-use-variable-from-cgi-with-hpricot/177056/3 "2009-10-11T06:53:46Z")

</div>

Wiley Davis wrote:

> I’m trying to get a url from a form and return the page title of said  
> url. I’m using cgi to get the web address from the url, which looks like  
> this:
> 
> http%3A%2F%[2Fpracticalpedal.com](http://2Fpracticalpedal.com)

> @linky = “[http://practicalpedal.com](http://practicalpedal.com)”

A cursory examination of those strings reveals that they are not  
identical.

> This works if I assign the @linky variable like so:  
> @linky = “[http://practicalpedal.com](http://practicalpedal.com)”

…and what about:

@linky = “http%3A%2F%[2Fpracticalpedal.com](http://2Fpracticalpedal.com)”

?? That’s the string you are getting from cgi. You can test that by  
writing @linky to a .txt file and then examining the contents of the  
file.

require ‘cgi’

str = “[http://practicalpedal.com](http://practicalpedal.com)”  
new\_str = CGI.escape(str)  
p new\_str

–output:–  
“http%3A%2F%[2Fpracticalpedal.com](http://2Fpracticalpedal.com)”

Look familiar?

x = CGI.unescape(new\_str)  
p x

–output:–  
“[http://practicalpedal.com](http://practicalpedal.com)”

The rule: certain characters have to be ‘transformed’ in order to pass  
them in a query string attached to the end of a url. That means the  
special characters have to be ‘untransformed’ on the other side.

---

<div class="post-metadata">

**Author:** ![wileydavis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b9bd4f/32.png) [@wileydavis](https://www.ruby-forum.com/u/wileydavis)\
**Post date:** [October 11, 2009, 4:02pm UTC](https://www.ruby-forum.com/t/cant-use-variable-from-cgi-with-hpricot/177056/4 "2009-10-11T16:02:09Z")

</div>

> A cursory examination of those strings reveals that they are not  
> identical.
> 
> The rule: certain characters have to be ‘transformed’ in order to pass  
> them in a query string attached to the end of a url. That means the  
> special characters have to be ‘untransformed’ on the other side.

Thanks for pointing out the unescape, something I hadn’t thought of.  
Unfortunately, as I’m using it at least, that didn’t fix the problem. It  
works when I do this:

@linky = “http%3A%2F%[2Fpracticalpedal.com](http://2Fpracticalpedal.com)”  
@linky = CGI.unescape(@linky)  
@result = open(@linky)  
@doc = Hpricot(@result)  
@title = (@doc/“title”).inner\_html  
@result.close  
puts @title

But it gives me a 500 Internal Server Error when I do this:

cgi = CGI.new  
@linky = cgi[‘link’]  
@linky = CGI.unescape(@linky)  
@result = open(@linky)  
@doc = Hpricot(@result)  
@title = (@doc/“title”).inner\_html  
@result.close  
puts @title

I’m pretty sure the @linky = cgi[‘link’] is working because when I  
remove the hpricot part of the code and just display the variable with a  
puts @linky, I get the correct string as output.

---

<div class="post-metadata">

**Author:** ![wileydavis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b9bd4f/32.png) [@wileydavis](https://www.ruby-forum.com/u/wileydavis)\
**Post date:** [October 12, 2009, 6:05am UTC](https://www.ruby-forum.com/t/cant-use-variable-from-cgi-with-hpricot/177056/5 "2009-10-12T06:05:56Z")

</div>

Wiley Davis wrote:

> > Other than that, your code looks ok to me, so I would check your form  
> > data. Something like the following should get recognized by cgi.
> 
> Just to be sure, I checked the form and it looks okay.
> 
> > CGI is a fickle little LIB, but she’ll start to do your bidding once she  
> > feels that you know what you are doing.
> 
> Darn… that’ll be a while 🙂
> 
> > If you just can’t get CGI to read the form data, you can try to access  
> > the data using ARGV as if the string was a command line variable to your  
> > script.
> 
> As noted below, cgi seems to be reading the string. I can read it and  
> display it with a:
> 
> puts @linky
> 
> but whenever i try to pass it along to hpricot, it gives me a server  
> error. Odd.

What’s the server error?

I installed eruby, and I was able to configure eruby to work with my own  
Apache2 install on mac osx 10.4.11,

(per the instructions in pickaxe2 p. 243 (a sym link didn’t work, I  
actually had to copy the file into cgi-bin))

and despite my earlier post, your code works fine for me using Apache  
and localhost, although you can use open() with a block, and then you  
don’t have to manually close the file:

## test.rhtml
 html page 
hi

world

\<% require 'rubygems' require 'cgi' require 'open-uri' require 'hpricot'

cgi = CGI.new  
linky = cgi[‘link’]

open(linky) do |f|  
doc = Hpricot(f)  
title = doc/“title”  
puts title.inner\_html  
end

%\>

–web browser displays:–  
hi  
world  
[pedalpower.com](http://pedalpower.com)

---

<div class="post-metadata">

**Author:** ![wileydavis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b9bd4f/32.png) [@wileydavis](https://www.ruby-forum.com/u/wileydavis)\
**Post date:** [October 11, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/cant-use-variable-from-cgi-with-hpricot/177056/6 "2009-10-11T16:05:43Z")

</div>

> Other than that, your code looks ok to me, so I would check your form  
> data. Something like the following should get recognized by cgi.

Just to be sure, I checked the form and it looks okay.

> CGI is a fickle little LIB, but she’ll start to do your bidding once she  
> feels that you know what you are doing.

Darn… that’ll be a while 🙂

> If you just can’t get CGI to read the form data, you can try to access  
> the data using ARGV as if the string was a command line variable to your  
> script.

As noted below, cgi seems to be reading the string. I can read it and  
display it with a:

puts @linky

but whenever i try to pass it along to hpricot, it gives me a server  
error. Odd.
