LWP - Upload via HTTP POST

M

Mark Mykkanen

Can someone tell me why the following code randomly produces a status
code of the following states?
500 Can't read entity body: Unknown error
405 Method not allowed
I'm trying to send a file via HTTP using a web form. I can upload a
file without any problems using a browser, but I haven't been
successful with the code below. Please help.

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

my $ua = new LWP::UserAgent;
$ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
my $req = new HTTP::Request 'GET' => 'http://localhost/';
$req->authorization_basic('user','pass');
my $content=$ua->request($req)->as_string;

=formOutput
<form ACTION="DataCollection.asp"
METHOD="POST"
ENCTYPE="multipart/form-data">

<input TYPE="File" NAME="File">
<input TYPE="Submit" VALUE="Submit">

</form>
=cut

# Everything up to here succeeds

my $res = $ua->post(
'http://localhost/',
Content_Type => 'form-data',
Content => [
file => ['data.txt']
]
);
print $res->content() if($res->is_success);
 
J

J. Gleixner

Mark said:
Can someone tell me why the following code randomly produces a status
code of the following states?
500 Can't read entity body: Unknown error
405 Method not allowed
I'm trying to send a file via HTTP using a web form. I can upload a
file without any problems using a browser, but I haven't been
successful with the code below. Please help.

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

my $ua = new LWP::UserAgent;
$ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
my $req = new HTTP::Request 'GET' => 'http://localhost/';
$req->authorization_basic('user','pass');
my $content=$ua->request($req)->as_string;

=formOutput
<form ACTION="DataCollection.asp"
METHOD="POST"
ENCTYPE="multipart/form-data">

<input TYPE="File" NAME="File">
<input TYPE="Submit" VALUE="Submit">

</form>
=cut

# Everything up to here succeeds

my $res = $ua->post(
'http://localhost/',
Content_Type => 'form-data',
Content => [
file => ['data.txt']
]
);
print $res->content() if($res->is_success);

Take a look at your error log, and you should see what the request is
trying to do.

The main issue is that the form has an action of "DataCollection.asp",
however your post method doesn't send it to that action.

There are other problems with your post request, look at

perldoc lwpcook

for examples.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top