How do I read HTTP POST XML sent to CGI?

Discussion in 'Ruby' started by Ting Chang, Apr 29, 2011.

  1. Ting Chang

    Ting Chang Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Ting Chang, Apr 29, 2011
    #1
    1. Advertising

  2. Ting Chang

    7stud -- Guest

    7stud --, Apr 29, 2011
    #2
    1. Advertising

  3. On Fri, Apr 29, 2011 at 10:46 PM, Ting Chang <> wrote:
    > 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.
     
    Jesús Gabriel y Galán, Apr 29, 2011
    #3
  4. Ting Chang

    Ting Chang Guest

    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!

    --
    Posted via http://www.ruby-forum.com/.
     
    Ting Chang, Apr 29, 2011
    #4
  5. Ting Chang

    Ting Chang Guest

    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.

    --
    Posted via http://www.ruby-forum.com/.
     
    Ting Chang, Apr 30, 2011
    #5
  6. Ting Chang

    7stud -- Guest

    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.

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 30, 2011
    #6
  7. Ting Chang

    Ting Chang Guest

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

    --
    Posted via http://www.ruby-forum.com/.
     
    Ting Chang, Apr 30, 2011
    #7
  8. Ting Chang

    7stud -- Guest

    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.

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 30, 2011
    #8
  9. Ting Chang

    Ting Chang Guest

    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!

    --
    Posted via http://www.ruby-forum.com/.
     
    Ting Chang, Apr 30, 2011
    #9
  10. On Sat, 30 Apr 2011, Ting Chang 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
    --
    Duke
     
    Duke Normandin, Apr 30, 2011
    #10
  11. Ting Chang

    7stud -- Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 30, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quentin Crain
    Replies:
    1
    Views:
    342
    Irmen de Jong
    Jun 26, 2004
  2. ColinK
    Replies:
    0
    Views:
    544
    ColinK
    Jul 15, 2007
  3. Stefan Fischer
    Replies:
    2
    Views:
    283
    Stefan Fischer
    Feb 23, 2010
  4. joe
    Replies:
    0
    Views:
    221
  5. gp
    Replies:
    1
    Views:
    109
Loading...

Share This Page