【问题标题】:How to test Perl CGI script for file upload from the command line?如何从命令行测试 Perl CGI 脚本以进行文件上传?
【发布时间】:2018-12-01 10:59:14
【问题描述】:

我有一个调用 my $file_handle = $cgi->upload('uploaded_file') 的 Perl 脚本。有没有办法在部署到 Web 服务器之前从命令行测试这个脚本?我查看了 (How can I send POST and GET data to a Perl CGI script via the command line?) 和其他问题,但找不到从命令行传递文件以供上传的任何信息。提前感谢您的帮助。

【问题讨论】:

  • 请提供上述脚本详细说明。您使用的是什么操作系统,哪个命令行?谢谢。
  • 您必须使用 Perl 来实际执行上传吗?如果不是,那么最简单的方法可能是使用curl - 如curl -T file https://yoursite.com/your-script.cgi
  • @David 要求他们已经使用 Web 服务器运行程序。他们想在没有网络服务器的情况下运行代码。

标签: perl file command-line upload cgi


【解决方案1】:

文件上传只是另一个 POST 请求!见DEBUGGING in CGI.pm。程序是index.pl,要上传的文件是somefile.bin,表单字段名是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
    '
)

【讨论】:

  • 你能解释一下&lt; &lt;(...)吗?为什么会有空间?
  • &lt;()tldp.org/LDP/abs/html/process-sub.html(避免临时文件或shell var); &lt; 从文件描述符重定向到进程标准输入。编辑:刚刚注意到,一个简单的管道也可以。
  • 这很好用。谢谢@daxim。我在这个 POST 方法中看到 Content_Type 设置为“form-data”,但是当我转储 $cgi 时,我看到:'Content-Disposition' => 'form-data; name="uploaded_file"; filename="wrapper_sbs.bin"', 'Content-Type' => 'application/octet-stream' 如果我将上传的文件扩展名更改为“.sh”,则 Content-Type 将变为“application/x-sh”。有什么方法可以在不考虑文件扩展名的情况下指定 Content-Type?
  • 当然,在documentation,向下滚动到Form-based File Upload
猜你喜欢
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
相关资源
最近更新 更多