# Parameter passed by get

**URL:** <https://www.ruby-forum.com/t/parameter-passed-by-get/173194>\
**Category:** Ruby\
**Created:** [August 4, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/parameter-passed-by-get/173194 "2009-08-04T19:39:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [August 4, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/parameter-passed-by-get/173194/1 "2009-08-04T19:39:54Z")

</div>

A third party has written a program that passes a parameter via the GET  
method. I need to use it to call a Ruby script and pass a parameter.

I have researched and experimented for days and cannot figure out how to  
retrieve a GET parameter in a Ruby script.

## Test code to pass the parameter is:
Test Get ---------------------------------------------
## I believe I should retrieve it with something like this:

#!/usr/bin/ruby

require ‘net/http’  
require ‘uri’

begin

print “Content-type: text/html\r\n\r\n”

server = URI.parse( ‘localhost/cgi-bin/test.rb’ )

http = Net::HTTP.start( server.host, server.port )

response = http.get( server.path )

print response.body

end

* * *

But I always receive ‘nil’. (I’ve tried many variations).

http.head( ‘[www.website.com](http://www.website.com)’ ) works and returns values as expected.

I’m sure this must be simple, but I have tried everything I can think of  
and can’t find an example anywhere.

Thanks to anyone who can assist.

-David

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [August 5, 2009, 8:50am UTC](https://www.ruby-forum.com/t/parameter-passed-by-get/173194/2 "2009-08-05T08:50:06Z")

</div>

David S. wrote:

> A third party has written a program that passes a parameter via the GET  
> method. I need to use it to call a Ruby script and pass a parameter.
> 
> I have researched and experimented for days and cannot figure out how to  
> retrieve a GET parameter in a Ruby script.
> 
> ## Test code to pass the parameter is:
> Test Get ---------------------------------------------
> ## I believe I should retrieve it with something like this:
> 
> #!/usr/bin/ruby
> 
> require ‘net/http’  
> require ‘uri’
> 
> begin
> 
> print “Content-type: text/html\r\n\r\n”
> 
> server = URI.parse( ‘localhost/cgi-bin/test.rb’ )
> 
> http = Net::HTTP.start( server.host, server.port )
> 
> response = http.get( server.path )
> 
> print response.body
> 
> end
> 
> * * *
> 
> But I always receive ‘nil’. (I’ve tried many variations).
> 
> http.head( ‘[www.website.com](http://www.website.com)’ ) works and returns values as expected.
> 
> I’m sure this must be simple, but I have tried everything I can think of  
> and can’t find an example anywhere.
> 
> Thanks to anyone who can assist.
> 
> -David

As far as I can make out, I would liken your html form and your program  
to this situation: you and a friend are standing next to each other  
facing a fence, and there is a fast flowing river on the other side of  
the fence. You tell your friend to throw baseballs over the fence.  
Then you open your baseball mitt and wonder why no baseballs are landing  
in your mitt.

If you want to catch some of those baseballs, you have to go to the  
other side of the fence. In ruby, you would do this:

#!/usr/bin/env ruby

require ‘cgi’

cgi = CGI.new  
value = cgi[“parameter”]

print “Content-type: text/html\r\n\r\n”

output =\<\<ENDOFHTML

Test
You sent: #{value} as a hidden parameter.
ENDOFHTML

print output
