开发者

Perl post request to send zip file as base64?

开发者 https://www.devze.com 2023-03-03 00:27 出处:网络
I have a Perl script trying to send a zip file like so with LWP UserAgent module my $req = POST $url, Content_Type => \'form-d开发者_JAVA技巧ata\',

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

my $req = POST $url, Content_Type => 'form-d开发者_JAVA技巧ata',
    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?


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消