# HTTP GET requests in a Ruby CGI Script

**URL:** <https://www.ruby-forum.com/t/http-get-requests-in-a-ruby-cgi-script/133227>\
**Category:** Ruby\
**Created:** [March 29, 2008, 2:40am UTC](https://www.ruby-forum.com/t/http-get-requests-in-a-ruby-cgi-script/133227 "2008-03-29T02:40:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 29, 2008, 2:40am UTC](https://www.ruby-forum.com/t/http-get-requests-in-a-ruby-cgi-script/133227/1 "2008-03-29T02:40:22Z")

</div>

Website: [http://unchartedmess.com/plaingoogle](http://unchartedmess.com/plaingoogle)  
I’ve attached the script code and the website is listed above. I’m  
working on a google frontend that filters the resulting html code from  
the google searches and displays a list of plaintext links. This is for  
some automation programs I am also working on.  
Is there a way (or work-around) to run a Net::HTTP.get\_response inside a  
CGI script. I want to submit the form, which passes two variables to the  
script. The script will run a google search, then sort and filter the  
resulting code. Then it will display the page with the results. Any help  
would be great, thanks.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 29, 2008, 4:33am UTC](https://www.ruby-forum.com/t/http-get-requests-in-a-ruby-cgi-script/133227/2 "2008-03-29T04:33:12Z")

</div>

Ben A. wrote:

> Website: [http://unchartedmess.com/plaingoogle](http://unchartedmess.com/plaingoogle)  
> I’ve attached the script code and the website is listed above. I’m  
> working on a google frontend that filters the resulting html code from  
> the google searches and displays a list of plaintext links. This is for  
> some automation programs I am also working on.  
> Is there a way (or work-around) to run a Net::HTTP.get\_response inside a  
> CGI script.

Sure. What problems are you having? The following ruby script works  
for me using Apache as my web server:

#!/usr/bin/env ruby

require ‘net/http’  
url = “[http://www.google.com](http://www.google.com)”  
resp = Net::HTTP.get\_response(URI.parse(url) )

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

The following is google’s web page:
\n”  
print resp.body

I put that file in the directory:

/Library/Apache2/cgi-bin

Subsequently, I can call that ruby script using the url:

[http://localhost/cgi-bin/ruby\_test.rb](http://localhost/cgi-bin/ruby_test.rb)

For instance, if I load this html page in my browser:

Ruby cgi test
[click me](http://localhost/cgi-bin/ruby_test.rb)

and then I click on the link, the ruby script executes and my browser  
displays:

The following is google’s web page:

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 29, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/http-get-requests-in-a-ruby-cgi-script/133227/3 "2008-03-29T20:39:52Z")

</div>

I think I’ve got it. I _think_ it was a problem with calling a function  
before sending the content-type bit. Darn. I had just worked up a php  
script linked to a sql database. Oh-well. The ruby script was a lot more  
flexible. Thanks!  
Here’s another question: Is is it possible to dynamically generate a php  
script? I would like to do the following:  
Take their search terms,  
Check if the terms are already in the database  
if they are -\> pull the results from the database and display them  
otherwise, pull the results out of google, and put them in the database,  
then display them.  
Not sure if you could go from a ruby script to a php page and back  
again. Generating the html content in the script is a b\*tch.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 30, 2008, 11:32am UTC](https://www.ruby-forum.com/t/http-get-requests-in-a-ruby-cgi-script/133227/4 "2008-03-30T11:32:20Z")

</div>

Ben A. wrote:

> I think I’ve got it. I _think_ it was a problem with calling a function  
> before sending the content-type bit. Darn. I had just worked up a php  
> script linked to a sql database. Oh-well. The ruby script was a lot more  
> flexible. Thanks!  
> Here’s another question: Is is it possible to dynamically generate a php  
> script?

Sure. php code is just text, and you can write text to a file in most  
computer programming languages. Here is a ruby example:

File.open(“prog1.php”, “w”) do |file|  
file.puts("\<?php echo 'hello' ?\>")  
end

> I would like to do the following:  
> Take their search terms,  
> Check if the terms are already in the database  
> if they are -\> pull the results from the database and display them  
> otherwise, pull the results out of google, and put them in the database,  
> then display them.  
> Not sure if you could go from a ruby script to a php page and back  
> again.

I think you should be able to execute a php script doing something like  
this:

system(“path1 path2”)

where path1 is the path to php and path2 is the path to prog1.php.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 30, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/http-get-requests-in-a-ruby-cgi-script/133227/5 "2008-03-30T19:51:01Z")

</div>

On Mar 29, 9:39 pm, Ben A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think I’ve got it. I _think_ it was a problem with calling a function  
> before sending the content-type bit. Darn.

Not sure about that, there were at least two errors in your script:  
0.upto(results-1) { |i|  
=\> 0.upto(results.to\_i-1) { |i|  
and:  
print tempbod.split(//).length-1  
=\> print body.split(//).length-1

Any error stop the script!

– Maurice

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 30, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/http-get-requests-in-a-ruby-cgi-script/133227/6 "2008-03-30T20:06:11Z")

</div>

mdiam wrote:

> On Mar 29, 9:39ï¿½pm, Ben A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I think I’ve got it. I _think_ it was a problem with calling a function  
> > before sending the content-type bit. Darn.
> 
> Not sure about that, there were at least two errors in your script:  
> 0.upto(results-1) { |i|  
> =\> 0.upto(results.to\_i-1) { |i|  
> and:  
> print tempbod.split(//).length-1  
> =\> print body.split(//).length-1
> 
> Any error stop the script!
> 
> – Maurice

The results is not a string, it’s an int. So thats not an error. The  
second print statement has been removed so it’s not an issue.

Actually, when I look at the code again, I had changed the call to that  
function from body = google(search, num) to body = google(search,  
num.to\_i)  
Everything works, more or less. And you can find the current page at  
[http://www.googleplainly.com](http://www.googleplainly.com) if you’re interested.
