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.
1 Answer
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
'
)
4 Comments
simbabque
Can you explain the
< <(...) please? Why is there a space?daxim
<() 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.BReddy
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?
daxim
Sure, that's in the documentation, scroll down to
Form-based File Upload.
curl- as incurl -T file https://yoursite.com/your-script.cgi.