1

I have a Perl script with a call my $file_handle = $cgi->upload('uploaded_file'). Is there a way to test this script from command line before deploying to the web server? I looked at (How can I send POST and GET data to a Perl CGI script via the command line?) and other questions, but could not find any information for passing a file for upload from the command line. Thanks in advance for the help.

BReddy
  • 407
  • 3
  • 13
  • Please elaborate by providing the said script. What OS are you using, which command line? Thanks. – Ahmad Bilal Jun 22 '18 at 03:43
  • 1
    Do you have to use Perl to actually perform the upload? If not then the easiest thing to do is probably use `curl` - as in `curl -T file https://yoursite.com/your-script.cgi`. – David Collins Jun 22 '18 at 07:55
  • @David that requires them to already run the program with a web server. They want to run the code without a web server. – simbabque Jun 22 '18 at 08:39

1 Answers1

3

A file upload is just another POST request! See DEBUGGING in CGI.pm. The program is index.pl, the file to upload is somefile.bin and the form field name is uploaded_file:

REQUEST_METHOD=POST \
CONTENT_TYPE='multipart/form-data; boundary=xYzZY' \
perl index.pl < <(
    perl -MHTTP::Request::Common=POST -e'
        print POST(
            undef,
            Content_Type => "form-data",
            Content => [ uploaded_file => [ "somefile.bin" ]]
        )->content
    '
)
daxim
  • 39,270
  • 4
  • 65
  • 132
  • Can you explain the `< <(...)` please? Why is there a space? – simbabque Jun 22 '18 at 10:04
  • 1
    `<()` is http://tldp.org/LDP/abs/html/process-sub.html (avoids temporary file or shell var) ; `<` redirects from file descriptor into the process stdin. Edit: just noticed, a simple pipe will also do. – daxim Jun 22 '18 at 11:29
  • This worked great. Thank you @daxim. I see Content_Type set to "form-data" in this POST method, but when I dump $cgi, I see: 'Content-Disposition' => 'form-data; name="uploaded_file"; filename="wrapper_sbs.bin"', 'Content-Type' => 'application/octet-stream' If I change the uploaded file extension to ".sh", the Content-Type is becoming 'application/x-sh'. Is there any way to specify the Content-Type without regard to the file extension? – BReddy Jun 22 '18 at 14:51
  • Sure, that's in the [documentation](http://p3rl.org/HTTP::Request::Common#POST-$url), scroll down to `Form-based File Upload`. – daxim Jun 23 '18 at 07:45