# Displaying the html from a ruby .cgi script

**URL:** <https://www.ruby-forum.com/t/displaying-the-html-from-a-ruby-cgi-script/180612>\
**Category:** Ruby\
**Created:** [December 22, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/displaying-the-html-from-a-ruby-cgi-script/180612 "2009-12-22T23:49:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [December 22, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/displaying-the-html-from-a-ruby-cgi-script/180612/1 "2009-12-22T23:49:20Z")

</div>

I have a script, foobar.cgi

I can load it into my browser, as in  
[http://localhost/foobar.cgi](http://localhost/foobar.cgi)

It generates html code. I am using Apache on Linux here,  
everything is local only.

My question is:

Is there an easy way to grab that generated html code and  
display it (as a long string) on a console?

I thought lynx -dump might help but it doesn’t, plus it is  
not a “ruby only” solution, which I would prefer (since  
I’d like this to work on windows as well, even without  
lynx around)

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [December 23, 2009, 3:04am UTC](https://www.ruby-forum.com/t/displaying-the-html-from-a-ruby-cgi-script/180612/2 "2009-12-23T03:04:30Z")

</div>

Marc H. wrote:

> I have a script, foobar.cgi
> 
> I can load it into my browser, as in  
> [http://localhost/foobar.cgi](http://localhost/foobar.cgi)
> 
> […]  
> Is there an easy way to grab that generated html code and  
> display it (as a long string) on a console?  
> […]  
> a “ruby only” solution, which I would prefer

require ‘open-uri’  
print open(‘[http://localhost/foobar.cgi](http://localhost/foobar.cgi)’).read

> I thought lynx -dump might help but it doesn’t,

What does “it doesn’t” mean? If it means “but I don’t see the HTML”,  
then that because you should’ve used -source, not -dump.

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [December 25, 2009, 8:17pm UTC](https://www.ruby-forum.com/t/displaying-the-html-from-a-ruby-cgi-script/180612/3 "2009-12-25T20:17:46Z")

</div>

On Dec 22, 2009, at 14:49 , Marc H. wrote:

> Is there an easy way to grab that generated html code and  
> display it (as a long string) on a console?

% ruby foobar.cgi \< /dev/null

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [December 23, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/displaying-the-html-from-a-ruby-cgi-script/180612/4 "2009-12-23T21:31:01Z")

</div>

Marc H. wrote:

> I have a script, foobar.cgi
> 
> I can load it into my browser, as in  
> [http://localhost/foobar.cgi](http://localhost/foobar.cgi)
> 
> It generates html code. I am using Apache on Linux here,  
> everything is local only.
> 
> My question is:
> 
> Is there an easy way to grab that generated html code and  
> display it (as a long string) on a console?

(1) curl [http://localhost/foobar.cgi](http://localhost/foobar.cgi)

With appropriate command-line options you can also POST to the CGI  
easily enough. (Maintaining a session with cookies would be more awkward  
though)

(2) I believe you should just be able to run the CGI from the command  
line:

/path/to/foobar.cgi  
or  
ruby /path/to/foobar.cgi

in which case it’ll prompt you for any parameters, then press ^D to get  
the response.

(3) The very manual way:

telnet localhost 80  
GET /foobar.cgi HTTP/1.0  
Host: localhost

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [January 30, 2010, 11:48am UTC](https://www.ruby-forum.com/t/displaying-the-html-from-a-ruby-cgi-script/180612/5 "2010-01-30T11:48:15Z")

</div>

Thanks for the help, works nicely!
