perl code snipet for issuing POST

Posted on October 22nd, 2006 by Chris Farris.
Categories: perl.

This took way to long to lookup how to do.

This issues a POST command to a cgi ($url), fetches the resulting content back and then parses the XML data.


use XML::Simple;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);

my $ua = LWP::UserAgent->new;

my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/x-www-form-urlencoded');
$req->content("Mac_Id=$mac");

my $res = $ua->request($req);
my $xml = $res->content;

# Parse cfgdata
my $xsl = XML::Simple->new();
my $config = eval { $xsl->XMLin($xml)} ;
if($@) { # bad XML
print "Invalid XML for $mac\n";
print ERROR "Invalid XML for $mac\n";
next;
}

0 comments.