Hi Ela,
The documentation for perl LWP agent seems sparse. I had a difficult time
figuring out how to send multipart form-data. I'll share the code with you that
some shared with me. Hope it helps.
require LWP;
use LWP::UserAgent;
use HTTP::Request::Common;
# Create a user agent object
$ua = new LWP::UserAgent;
$ua->agent("AgentName/0.1 " . $ua->agent);
# Pass request to the user agent and get a response back
my $res = $ua->request (POST $URL, Content_Type => 'form-data', Content => [
login_id => $Username,
login_passwd => $Password,
name_auth => $Prefix,
fname => ["$XML_Dir\\$XML_File"],
operation => 'Submit Batch File',
]);
# Check the outcome of the response - I guess we just file away
if ($res->is_success) {
print "success!\n";
print $res->content;
if ( $res->content =~ /\Q<H2>SUCCESS<\/H2>\E/i ) {
print "Deposit successful\n";
} else {
print POSTLOG "Deposit FAILED.\n";
}
} else {
print " failed!\n";
}
Ela Jarecka wrote:
> Hi,
> I am using the following code to send and XML document ( output.xml ) to a
> remote server:
>
> use strict;
> use LWP::Debug qw(+);
> use LWP::UserAgent;
> use IO;
>
> my $resp;
> $resp = 'response.xml';
> my $FILEH;
> open (FILEH, <output.xml>) or die "Can't open file output.xml!\n";
>
> my $ua = LWP::UserAgent->new;
>
> #another version that i've tried...
> #my $h = new HTTP::Headers Date=> '2001-05-18';
> #my $req =
> HTTP::Request->new('POST','http://195.252.142.171:8008',$h,$FILEH);
>
> my $req = HTTP::Request->new(POST => 'http://195.252.142.171:8008');
>
> #$req->content_type('text/xml');
> $req->content($FILEH);
>
> my $res = $ua->request($req,$resp); <--------------------here I've also
> tried plain request($req) but the result is the same
> if ( $res->is_success) {
> print "OK!\n";
> #print $res->as_string;
> } else {
> print "Failed: ", $res->status_line, "\n";
> }
>
> And that's what I get:
>
> LWP::UserAgent::new: ()
> LWP::UserAgent::request: ()
> LWP::UserAgent::simple_request: POST http://195.252.142.171:8008/
> LWP::UserAgent::_need_proxy: (http://195.252.142.171:8008/)
> LWP::UserAgent::_need_proxy: Not proxied
> LWP::Protocol::http::request: ()
> LWP::Protocol::http::request: POST / HTTP/1.0
> Host: 195.252.142.171:8008
> User-Agent: libwww-perl/5.21
>
> LWP::Protocol::http::request: reading response
> LWP::UserAgent::request: Simple result: Internal Server Error
> Failed: 500 read timeout
>
> #######
> Could anyone please help me? The problem is that I am not too sure whether
> my request is correct in the first place.
> In the manuals, $content is described as 'an arbitrary amount of data'.. Is
> my filehandle properly interpreted? I've tried
> using only the name of the file, but obviously it didn't work, being
> interpreted as a 10 chars long string...
>
> Thanks in advance,
> Ela