tags:

views:

539

answers:

3

Hi,

I want to send some XML from a Perl program to a CGI script that makes use of XML::Simple to take that XML as input and send XML as output.

Is there a way to send XML to a CGI script from Perl? Any help in this regards would be really appreciated.

Thank You

A: 

Assuming you have the XML in your program already; it is just an HTTP request, so LWP is your friend. The specifics depend on how the CGI program expects the XML to be passed (e.g. as POSTed url-encoded-form-data, multi-part MIME, etc)

David Dorward
A: 

There's nothing special about XML: it's just text. Send it like you would send any other text. Is there something else that isn't working for you? What have you tried already?

If you're having trouble sending anything to the CGI program, take a look at a framework such as WWW::Mechanize which does most of the work of the request and response loop for you.

brian d foy
+1  A: 

One of the possible solutions would be use the HTTP::Request::Common module, which exposes some useful functions like GET, POST and HEADER.

Assuming you want to use POST to send the data to the remote application, you could do:

use HTTP::Request::Common;
use LWP::UserAgent;

my $url = 'http://localhost/cgi-bin/mycgi.pl';
my $xml = "<root></root>";
my $request = POST $url, Content_Type => 'text/xml; charset=utf-8', Content => $xml;
my $ua = LWP::UserAgent->new();
my $response = $ua->request($request);
if ( $response->is_success() ) {
    print $response->content();
}
else {
    warn $response->status_line, $/;
}

Hope this helps!

Igor