Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

http upload simulating browser

by marauder (Novice)
on Dec 06, 2001 at 23:57 UTC ( #130045=perlquestion: print w/replies, xml ) Need Help??

marauder has asked for the wisdom of the Perl Monks concerning the following question:

why doesn't this work? I am trying to upload a file to a webserver without actually doing it through a broweser but through a perl script. Here's the code;

use CGI; use HTTP::Request::Common; use LWP; $ua = LWP::UserAgent->new; my $res= $ua->request(POST '', [description => 'a description', filetoupload => 'c:/test.dat' ]); if ($res->is_success) {print "success\n";} else {print "failed $!\n";}

I always get "failed invalid argument". Any help much appreciated. by the way sqware brackets are included but don't show in the code above.

2001-12-06 Edit by Corion : Added Code tags

Replies are listed 'Best First'.
Re: http upload simulating browser
by miyagawa (Chaplain) on Dec 07, 2001 at 00:36 UTC
    you should use CODE tag.

    File uploading via LWP is done like this (quote from perldoc: HTTP::Request::Common)

    POST '', Content_Type => 'form-data', Content => [ name => 'Gisle Aas', email => '', gender => 'M', born => '1964', init => ["$ENV{HOME}/.profile"], ]
    the point is
  • set content-type to multipart/form-data
  • use arrayref to indicate file upload

    Tatsuhiko Miyagawa

Re: http upload simulating browser
by IlyaM (Parson) on Dec 07, 2001 at 00:37 UTC
    Read docs for HTTP::Request::Common. You should have something like (untested):
    use HTTP::Request::Common; use LWP; my $ua = LWP::UserAgent->new; my $res = $ua->request(POST '', Content_Type => 'form-data', Content => [ description => 'a description', filetoupload => ['c:/test.dat'] ]); .... ....
    Note that you probably do not need to use CGI if you want to write HTTP client.

    Ilya Martynov (

Re: http upload simulating browser
by Fastolfe (Vicar) on Dec 07, 2001 at 01:03 UTC
    $! does not contain a useful error message in this context. $! is meant for system error messages. Since the $res object neatly encapsulates the HTTP::Response from the web server, you should try printing this out instead of looking for an error message. (Technically, this isn't an application error, it's a "failure" response from a web server.)
Re: http upload simulating browser
by jlongino (Parson) on Dec 07, 2001 at 01:40 UTC
    You could also do it this way:
    use strict; require LWP::UserAgent; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new('GET', ' +cgi'); my $resp = $ua->request($req, 'c:/test.dat'); if ($resp->is_success) { print "Success.\n"; } else { print "Failed.\n"; }

    Update: Sorry about that. I misread the original post and was thinking download.

      You cannot upload files with GET request. And second optional parameter of $us->request is not a file to upload but is a file to save response.

      Ilya Martynov (

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://130045]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2023-12-06 11:21 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (30 votes). Check out past polls.