-1

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?

  • Show your upload code if you want us to point out what's wrong with it! – ikegami Sep 15 '17 at 07:25
  • @ikegami I even don't understand how should i get the file name. As usual I got a filename in QUERY_STRING and than write it to the server `my $filehandle = $self->{cgiQuery}->param('filename'); my $filename = $xmlrootpath.$cgiData->{filename}; my $buffer; open (OUTFILE,"> $filename"); while (my $bytesread = read($filehandle, $buffer, 1024)) { print OUTFILE $buffer; } close OUTFILE;` – Artem Bardachov Sep 15 '17 at 07:31
  • There is no file name to be had. Now please fulfill my request. – ikegami Sep 15 '17 at 07:37

2 Answers2

2

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> };
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Yes, i need receive file from the `application/octet-stream` request and save it to the server. What I should do after `my $file = $cgi->param('POSTDATA');` – Artem Bardachov Sep 15 '17 at 07:39
  • Nothing. That places the file (the body of the request) in `$file`. There is nothing else to do. – ikegami Sep 15 '17 at 07:40
  • But there is content within the **offers0_14.xml**. There is a big XML Inside this file. How should I get that content ? – Artem Bardachov Sep 15 '17 at 07:43
  • If you want the contents of `offers0_14.xml`, maybe you should post that instead of `type=catalog&mode=file&filename=offers0_14.xml`. – ikegami Sep 15 '17 at 07:45
  • Actually, that's the problem. This request send me the client 1C:Enterprise and he claims that there is a full XML inside the offers0_14.xml – Artem Bardachov Sep 15 '17 at 08:30
0

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;