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.

3
  • Please elaborate by providing the said script. What OS are you using, which command line? Thanks. Commented Jun 22, 2018 at 3: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. Commented Jun 22, 2018 at 7:55
  • @David that requires them to already run the program with a web server. They want to run the code without a web server. Commented Jun 22, 2018 at 8:39

1 Answer 1

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
    '
)
Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain the < <(...) please? Why is there a space?
<() is 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.
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?
Sure, that's in the documentation, scroll down to Form-based File Upload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.