Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Finding XML POST data in HTTP::Server::Simple::CGI

by Wiggins (Hermit)
on Mar 16, 2016 at 16:26 UTC ( [id://1157961]=perlquestion: print w/replies, xml ) Need Help??

Wiggins has asked for the wisdom of the Perl Monks concerning the following question:

Well folks, I had hoped to be able to do this in just a couple of day by modifying a prior sample. No such luck.

I want to make a Restfull server that uses mostly canned (I tweak with current data and message number with regex substitutions) answers to 2 procedure names. It would be a "TAXII Discovery Service".

TAXII is XML, so HTTP POST 'queries' are irrelevant, but the HTTP heading, and the XML body are critical.

So far I have been working off of the large example in this module.
http://search.cpan.org/~bps/HTTP-Server-Simple-0.51/lib/HTTP/Server/Simple.pm

I insert into the 'resp_hello()' subroutine reading STDIN(empty), or $cgi->param('POSTDATA') (empty string), or `set>/tmp/environment.txt` (no XML variables).

The input I send to the interface is this:

POST /hello HTTP/1.1 Proxy-Connection: keep-alive Host: 127.0.0.1 Content-type: application/xml Accept: application/xml Cache-Control: no-cache X-TAXII-Services: urn:taxii.mitre.org:services:1.1 X-TAXII-Protocol: urn:taxii.mitre.org:protocol:http:1.0 Accept-Language: en-US,en;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KH +TML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 Content-length: 98 <Discovery_Request xmlns="http://taxii.mitre.org/messages/taxii_xml_bi +nding-1.1" message_id="1"/>
My mods to the example are :
my $who = $cgi->param('name'); my $msg = $cgi->param('POSTDATA'); my @keywords = $cgi->keywords; print join(',',@keywords); print "this is a simple text string who=<$who> param('POSTDATA')=<< +$msg>>>\n"; print $cgi->header, $cgi->start_html("Hello"), $cgi->h1("Hello $who!"), $cgi->end_html; }
I get in return:
HTTP/1.0 200 OK this is a simple text string who=<> param('POSTDATA')=<<>>> Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Hello</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <h1>Hello !</h1> </body> </html>
(Note that this output shows the $who variable was also an enpty string!)

Based upon the CGI.pm page (http://perldoc.perl.org/CGI.html):

If POSTed data is not of type application/x-www-form-urlencoded or multipart/form-data, then the POSTed data will not be processed, but instead be returned as-is in a parameter named POSTDATA. To retrieve it, use code like this: my $data = $query->param('POSTDATA');
Of course, all documentation of creating a 'query' is the result of a CGI->new() sort of thing. But the page for the server says the input arg is a CGI object (already made).

I can't find the Body line ('<Discovery_Request...>') reading STDIN, nor the param() approach above, nor in any environment variables.

I chose this module, because it appeared simple, had a good example, and didn't involve huge amounts of study to just install it. I also thought it would perform as the limited docs repesented.

It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Replies are listed 'Best First'.
Re: Finding XML POST data in HTTP::Server::Simple::CGI
by Anonymous Monk on Mar 16, 2016 at 16:47 UTC

    I think you'll need to provide us with a runnable example that reproduces the problem (see http://sscce.org/). POSTDATA works fine for me:

    { package SimpleSrv; use parent 'HTTP::Server::Simple::CGI'; sub handle_request { my ($self,$cgi) = @_; print "HTTP/1.1 200\x0D\x0A"; print $cgi->header(-type=>'text/plain'); print "<<<".($cgi->param('POSTDATA')//"undef").">>>\n"; } } my $server = SimpleSrv->new(8080); $server->host('127.0.0.1'); $server->run();

    Example:

    $ telnet localhost 8080 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. POST /hello HTTP/1.1 Content-type: text/plain Content-length: 6 foobar HTTP/1.1 200 Content-Type: text/plain; charset=ISO-8859-1 <<<foobar>>> Connection closed by foreign host.
      Try with:
      Content-type: application/xml

      It is always better to have seen your target for yourself, rather than depend upon someone else's description.

        Ahh ... that bad decision of CGI tying the mime-type of application/xml to XFORMS ... use text/xml instead.

        -derby

        Um, no, you try it and tell us what problems you had. (Because it works perfectly fine on my end, even with application/xml and even with the request in your first post.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1157961]
Approved by jellisii2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found