Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl post request to send zip file as base64?

Tags:

perl

lwp

I have a Perl script trying to send a zip file like so with LWP UserAgent module

my $req = POST $url, Content_Type => 'form-data',
    Content      => [
        submit => 1,
        upfile =>  [ $fname ]
    ];

where $fname is the path of the file. On the server side though it seems my POST array only has "submit". Should I base64 encode the file and assign it to a variable? What's the best way to do this?

like image 370
user391986 Avatar asked Dec 01 '25 03:12

user391986


1 Answers

Make sure the filename can be resolved. You should get an error if it cannot be, though. At least I do in my version of HTTP::Request::Common.

You don't have to encode the binary content as Base64. (Unless, of course, the server-side app happens to expect that format.)

Here's a complete sample script:

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common 'POST';

my $ua = LWP::UserAgent->new;
my $url = 'http://localhost:8888'; # Fiddler
my $req = POST $url,
    Content_Type => 'form-data',
    Content => [
        submit  => 1,
        upfile  => [ 'C:\temp\bla.zip' ],
    ];
my $line = '=' x 78 . "\n";
print $line, $req->as_string;
my $rsp = $ua->request( $req );
print $line, $rsp->as_string;
like image 196
Lumi Avatar answered Dec 03 '25 16:12

Lumi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!