How to upload a file from POST request. CONTENT_TYPE: application/octet-stream. Perl

1.4k views Asked by At

I get a POST request with

CONTENT_TYPE: application/octet-stream

I get all data like this

my $cgiQuery = CGI->new() or die();
my $cgiData = $cgiQuery->Vars;

my $getQuery = CGI->new($ENV{QUERY_STRING});
my $getData = $getQuery->Vars;

foreach my $getKey(keys %{$getData})
{
    $cgiData->{$getKey} = $getData->{$getKey};
}

my $data = $cgiQuery->param('POSTDATA');

But when i try to print it

print Dumper($data) ."\n";
print Dumper($cgiData) ."\n";

I get this:

$VAR1 = 'type=catalog&mode=file&filename=offers0_14.xml';

$VAR1 = {
      'POSTDATA' => 'type=catalog&mode=file&filename=offers0_14.xml',
      'session' => 'aa4979ad18f64e4d959dd16444cee5fd'
    };

How can i get and upload file filename=offers0_14.xml to the server?

2

There are 2 answers

0
Artem  Bardachov On BEST ANSWER

The solution is next, That POSTDATA is binary file. So, i got that file by this code

my $file_cgi = CGI->new($ENV{POSTDATA});
my $file = $file_cgi->Vars;

my $filename = path_to_the_file_on_the_server;    
open (OUTFILE,"> $filename");
print OUTFILE $file->{POSTDATA};
close OUTFILE;
5
ikegami On

If you want to post a file:

open(my $fh, '<:raw', $qfn)
   or die $!;

my $file = do { local $/; <$fh> };

$ua->post($uri,
   Content_Type => 'application/octet-stream',
   Content => $file,
);

To receive it:

my $file = $cgi->param('POSTDATA');

If you want to post a form that includes a file upload field:

$ua->post($uri,
   Content_Type => 'form-data',
   Content => [
      type  => 'catalog',
      mode  => 'file',
      file  => [ $qfn ],
   ],
);

To receive it:

my $fn = $cgi->param('file');  # Not safe to use!!!
my $fh = $cgi->upload('file');
my $file = do { local $/; <$fh> };