# How do I read HTTP POST XML sent to CGI?

**URL:** <https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400>\
**Category:** Ruby\
**Created:** [April 29, 2011, 10:46pm UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400 "2011-04-29T22:46:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 29, 2011, 10:46pm UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/1 "2011-04-29T22:46:29Z")

</div>

Hello Ruby Masters,

I am a ruby newbie and tried to use a http client to send the XML HTTP  
POST request to my cgi,  
I thought I can get the text I input in the HTTP POST directly from the  
$stdin as a string in cgi and extract the data out. but it didn’t seem  
to work.

I tried to do $stdin.realines to parse every line of the $stdin but I  
got nothing out of it.

Could anyone please advise ?

Thanks,  
Erick

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 29, 2011, 11:07pm UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/2 "2011-04-29T23:07:30Z")

</div>

You don’t read from $stdin. Read this cgi tutorial:

> **[Ruby Web Applications - CGI Programming](http://www.tutorialspoint.com/ruby/ruby_web_applications.htm)**
>
> Ruby Web Applications CGI Programming - Ruby is a general-purpose language; it can't properly be called a web language at all. Even so, web applications and web tools in general are among the most common uses of Ruby.

Note that there is no distinction between POST and GET data when using  
cgi: you read them the same way with your ruby script.

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 29, 2011, 11:16pm UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/3 "2011-04-29T23:16:45Z")

</div>

On Fri, Apr 29, 2011 at 10:46 PM, Ting C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Could anyone please advise ?

Are you using the cgi library from the stdlib?  
If so, it reads the params from $stdin and gives you access to them  
as a hash. Take a look at this example:

[[http://www.tutorialspoint.com/ruby/ruby\_web\_applications.htm](http://www.tutorialspoint.com/ruby/ruby_web_applications.htm)]

#!/usr/bin/ruby

require ‘cgi’  
cgi = CGI.new  
h = cgi.params # =\> {“FirstName”=\>[“Zara”],“LastName”=\>[“Ali”]}  
h[‘FirstName’] # =\> [“Zara”]  
h[‘LastName’] # =\> [“Ali”]

It takes care of GET and POST parsing either the query string or the  
POST body for you.

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 29, 2011, 11:54pm UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/4 "2011-04-29T23:54:33Z")

</div>

Thanks all, does that mean if I POST

apple  
ipad

in the HTTP Client then I can read it in my cgi as:

#!/usr/bin/ruby

require ‘cgi’  
cgi = CGI.new  
h = cgi.params # =\> {“customer”=\>[“apple”],“product”=\>[“ipad”]}  
h[‘customer’] # =\> [“apple”]  
h[‘product’] # =\> [“ipad”]

Thanks!

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 29, 2011, 11:57pm UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/5 "2011-04-29T23:57:32Z")

</div>

On Sat, Apr 30, 2011 at 05:46:50AM +0900, Ting C. wrote:

> Could anyone please advise ?

Use the CGI library.

```
require 'cgi'
cgi = CGI.new
cgi['parameter_name']

```

Note: This is from memory. I have not tested it.

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 30, 2011, 1:02am UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/6 "2011-04-30T01:02:16Z")

</div>

I am using this tool:

> **[soft-net.net](http://ww38.soft-net.net/SendHTTPTool.aspx)**
>
> Free ASP Net Hosting

and simply post Text input with the Method and URL specified only.  
And the text content I input is:.

 www.xxxurl.com DATA\_TYPE IMAGE 

I wish in the cgi code to extract the URL and the option values out but  
by following the reference above I cannot get it work.

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 30, 2011, 1:16am UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/7 "2011-04-30T01:16:16Z")

</div>

Ting C. wrote in post #995885:

> I am using this tool:  
> [http://soft-net.net/SendHTTPTool.aspx](http://soft-net.net/SendHTTPTool.aspx)  
> and simply post Text input with the Method and URL specified only.  
> And the text content I input is:.
> 
> www.xxxurl.com DATA\_TYPE IMAGE 
> 
> I wish in the cgi code to extract the URL and the option values out but  
> by following the reference above I cannot get it work.

Post your ruby code.

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 30, 2011, 1:24am UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/8 "2011-04-30T01:24:40Z")

</div>

well… i only followed the instruction. trying to see if those  
parameters came in

#!/usr/local/bin/ruby  
require “cgi”

cgi = CGI.new

h = cgi.params  
puts h[‘URL’]  
puts h[‘VALUE’]

puts cgi[‘PARAMETER’]

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 30, 2011, 1:31am UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/9 "2011-04-30T01:31:17Z")

</div>

Ting C. wrote in post #995889:

> well… i only followed the instruction. trying to see if those  
> parameters came in

Nobody on the ruby forum has any idea what your software does with this:

> www.xxxurl.com DATA\_TYPE IMAGE

A request containing post data is sent to the server with name/value  
pairs in the body of the request. Maybe your  
software interprets that xml as an instruction to send a request to  
[www.xxxurl.com](http://www.xxxurl.com), with the name/value pairs DATA\_TYPE=IMAGE.

By the way, you can also get the individual parameters like this:

puts cgi[“DATA\_TYPE”]

And you might want to do a:

p cgi.params

to see if you get any post data.

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 30, 2011, 2:21am UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/10 "2011-04-30T02:21:53Z")

</div>

Sorry about misleading, I think my problem is that I cannot get any POST  
data in my CGI, I tried your suggestion above and looks like the POST  
data did not come through.

do you know what’s possibly the reason?  
or do you have any format recommend in the POST data? the only thing  
that matter to me is to get the url data so my cgi script can take that  
url to do the rest of the work.

Thanks!

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 30, 2011, 3:36am UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/11 "2011-04-30T03:36:12Z")

</div>

On Sat, 30 Apr 2011, Ting C. wrote:

> Sorry about misleading, I think my problem is that I cannot get any POST  
> data in my CGI, I tried your suggestion above and looks like the POST  
> data did not come through.
> 
> do you know what’s possibly the reason?  
> or do you have any format recommend in the POST data? the only thing  
> that matter to me is to get the url data so my cgi script can take that  
> url to do the rest of the work.

Did you read this:

[http://www.tutorialspoint.com/ruby/ruby\_web\_applications.htm](http://www.tutorialspoint.com/ruby/ruby_web_applications.htm)

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 30, 2011, 7:51am UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/12 "2011-04-30T07:51:23Z")

</div>

On Sat, Apr 30, 2011 at 09:21:55AM +0900, Ting C. wrote:

> Sorry about misleading, I think my problem is that I cannot get any POST  
> data in my CGI, I tried your suggestion above and looks like the POST  
> data did not come through.
> 
> do you know what’s possibly the reason?  
> or do you have any format recommend in the POST data? the only thing  
> that matter to me is to get the url data so my cgi script can take that  
> url to do the rest of the work.

Try this as a way to diagnose the issue:

```
require 'cgi'
cgi = CGI.new
cgi.params.each_pair {|k,v| puts k + ' => ' + v }

```

That should tell you exactly what parameter keys and values are being  
sent to your Ruby script. If you’re actually viewing the output in the  
browser, you might want to try adding some markup:

```
require 'cgi'
cgi = CGI.new
puts "<ul>"
cgi.params.each_pair {|k,v| puts "<li>#{k} => #{v}</li>" }
puts "</ul>"

```

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [April 29, 2011, 11:59pm UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/13 "2011-04-29T23:59:23Z")

</div>

On Sat, Apr 30, 2011 at 06:54:36AM +0900, Ting C. wrote:

> cgi = CGI.new  
> h = cgi.params # =\> {“customer”=\>[“apple”],“product”=\>[“ipad”]}  
> h[‘customer’] # =\> [“apple”]  
> h[‘product’] # =\> [“ipad”]

Show us an example of how you’re posting the data.

---

<div class="post-metadata">

**Author:** ![erick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@erick](https://www.ruby-forum.com/u/erick)\
**Post date:** [May 1, 2011, 12:35am UTC](https://www.ruby-forum.com/t/how-do-i-read-http-post-xml-sent-to-cgi/206400/14 "2011-05-01T00:35:48Z")

</div>

Ting C. wrote in post #995897:

> Sorry about misleading, I think my problem is that I cannot get any POST  
> data in my CGI, I tried your suggestion above and looks like the POST  
> data did not come through.
> 
> do you know what’s possibly the reason?

Do you have to use that request software? You can easily test whether  
your server’s cgi gateway is working by using a ruby script to send the  
post request:

require ‘net/http’  
require ‘uri’

url = URI.parse(‘[http://www.somehost.com/some\_page](http://www.somehost.com/some_page)’)  
#That should be the url to your cgi script

data = {  
‘my\_url’ =\> ‘http://www.some\_site.com’  
}  
#That hash should include all name/value pairs you  
#want to send to the server

response = Net::HTTP.post\_form(url, data)

puts response.body

That will also work if both scripts are on localhost–just start up your  
server script, then in a different terminal window execute your request  
script.

You should start by writing a cgi script that just echos back the info  
it received, like Chad P. showed you. Once you know that everything  
is working correctly with the cgi gateway, then you can figure out why  
your original script isn’t working.
