Search code examples
perlweb-serviceshttplwp-useragent

How to POST a local file using Perl?


I have a server running a Perl webservice. This webservice generates a ~75mb .exe file.

I wish to make a POST request to send this file data to another webserver.

Right now I am using LWP::UserAgent like so:

use LWP::UserAgent;

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

# URL to post to
my $url = "http://my.website.here.com/upload";

# Location of local file
my $file_path = "/path/to/file.exe";

# Make the POST request
my $req = $ua->post(
    $url,
    [ Content_Type => 'form-data', 'file' => [$file_path] ]
);

Right now, however, only the file name is being sent. I can see why that would be the case, but what am I missing here?

Many thanks!


Solution

  • Refer to HTTP::Request::Common for the arguments to ->get and ->post.

    my $req = $ua->post($url,
        [ Content_Type => 'form-data', 'file' => [$file_path] ]
    );
    

    should be

    my $req = $ua->post($url,
        Content_Type => 'form-data',
        Content => [
            file => [$file_path],
        ],
    );