john
john

Reputation:

How can I send XML to a CGI program from Perl?

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

Upvotes: 2

Views: 3358

Answers (3)

Igor
Igor

Reputation: 4848

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!

Upvotes: 2

brian d foy
brian d foy

Reputation: 132914

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.

Upvotes: 0

Quentin
Quentin

Reputation: 944521

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)

Upvotes: 0

Related Questions