How do I read HTTP POST XML sent to CGI?

T

Ting Chang

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
 
J

Jesús Gabriel y Galán

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 ?

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]

#!/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.
 
T

Ting Chang

Thanks all, does that mean if I POST

<customer>apple</customer>
<product>ipad</product>

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!
 
T

Ting Chang

I am using this tool:
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:.

<QUERY CMD="LOOKUP">
<URL>www.xxxurl.com</URL>
<OPTION>
<PARAMETER>DATA_TYPE</PARAMETER>
<VALUE>IMAGE</VALUE>
</OPTION>
</QUERY>

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.
 
7

7stud --

Ting Chang wrote in post #995885:
I am using this tool:
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:.

<QUERY CMD="LOOKUP">
<URL>www.xxxurl.com</URL>
<OPTION>
<PARAMETER>DATA_TYPE</PARAMETER>
<VALUE>IMAGE</VALUE>
</OPTION>
</QUERY>

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.
 
T

Ting Chang

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']
 
7

7stud --

Ting Chang 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:
<QUERY CMD="LOOKUP">
<URL>www.xxxurl.com</URL>
<OPTION>
<PARAMETER>DATA_TYPE</PARAMETER>
<VALUE>IMAGE</VALUE>
</OPTION>
</QUERY>

Post data is sent to the server as name/value pairs. Maybe your
software interpretes that xml as an instruction to send a request to
www.xxxurl.com, with the name/value pairs of DATA_TYPE=IMAGE.
 
T

Ting Chang

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!
 
D

Duke Normandin

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
 
7

7stud --

Ting Chang 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 to your cgi script:

require 'net/http'
require 'uri'

url = URI.parse('http://www.somehost.com/some_page')

data = {
'url' = 'http://www.some_site.com'
}

response = Net::HTTP.post_form(url, data)

puts response.body
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top