Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Question: Send a chunk of XML to a server

by lihao (Monk)
on Nov 09, 2009 at 17:46 UTC ( [id://805972]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, monks

My co-worker wrote an API that requests me to send a chuck of XML to a specific server(i.e. http://example.com/api/action.html?xml) and then that server can execute some DB read/write accordingly.. a sample XML is shown below:

<api> <protocol>xml</protocol> <user>lihao</user> <key>**************</key> <data> <v1>Value 1</v1> <v2>Value 2</v2> <v3>Value 3</v3> </data> </api>

From my understanding, this is not like a typical HTTP form POST and also the XML is not in a typical SOAP format(I am not familiar with Web Service stuff though). Is there any easy way for me to use a Perl Module to send this chunk of data to that server directly??

Many thanks

lihao

Replies are listed 'Best First'.
Re: Question: Send a chunk of XML to a server
by derby (Abbot) on Nov 09, 2009 at 18:21 UTC

    Sure. LWP can do this for you.

    my $req = HTTP::Request->new( POST => 'http://some.site/app' ); $req->content_type( 'text/xml' ); $req->content( $myxml ); ...
    It's up to the server side on how to interpret the data coming to them. They may or may not need the content type set to 'text/xml'. If they're using CGI, you need to set the content_type to something other than 'application/x-www-form-urlencoded' or 'multipart/form-data'. In this example, by setting the content_type to 'text/xml', CGI will not try to parse the incoming stream (See the section 'HANDLING NON-URLENCODED ARGUMENTS' in CGI). But ... if they used something other than perl or CGI, they should tell you what the content_type should be.

    -derby

    Update I probably wasn't clear. There is no inherent tie between a POST and forms. Forms are normally POSTed (but they don't need to be) and POSTs are normally handed off to form processors. But that's just convention. Just as you can use a GET to send form data (not that I recommend it), you can use a POST to do thinks other than form processing.

      s/convention/CGI protocol/
        No, derby is correct. The CGI protocol is agnostic to type of data being posted. It doesn't have to be form data. That's why it provides a means of specifying the type of the content being posted (env var CONTENT_TYPE).

        There's nothing in the CGI specification that says a form submission must be a POST or that *all* POST submissions are forms. Am I missing something AM?

        -derby
Re: Question: Send a chunk of XML to a server
by ikegami (Patriarch) on Nov 09, 2009 at 17:58 UTC

    this is not like a typical HTTP form POST

    It's easiest to make it one. Pick a param name (say "request") and set that for the value of the param. Then you can just use $cgi->param('request') on the server side to obtain the XML.

      But I can not change the server-side code which was written in PHP by other developers, and they wanted me to send exactly that XML data to the server. sounds to me that it can not be handled by a form POST.

        I was trying to save you some trouble writing the server. Since that's already done, just use

        my $response = $ua->post( 'http://...', Content_Type => 'application/xml', Content => $xml, );
        Or if you need to twiddle with the request object,
        use HTTP::Common::Request qw( POST ); my $request = POST( "http://...", Content_Type => 'application/xml', Content => $xml, ); my $response = $ua->request($request);

        sounds to me that it can not be handled by a form POST.

        Of course you can't avoid using a form by using a form. I fail to see your point.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-25 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found