即时尝试使用系统curl将gzip压缩数据发布到服务器,但我总是遇到奇怪的错误
`curl -sS -X POST -H "Content-Type: application/gzip" --data-binary $data $url`
给
curl: no URL specified!
和
`curl -sS -X POST -H "Content-Type: application/gzip" --data-binary "$data" $url`
给
sh: -c: line 0: unexpected EOF while looking for matching `"' sh: -c: line 1: Syntax error: unexpected end of file
解决方法
添加“是朝着正确方向迈出的一步,但您没有考虑$data可能包含”,$等.您可以使用
String::ShellQuote来解决问题.
use String::ShellQuote qw( shell_quote ); my $cmd = shell_quote( curl => ( '-sS','-X' => 'POST','-H' => 'Content-Type: application/gzip','--data-binary' => $data,$url,),); my $output = `$cmd`;
或者你可以完全避开外壳.
my @cmd = ( curl => ( '-sS',); open(my $pipe,'-|',@cmd) or die $!; my $output = do { local $/; <$pipe> }; close($pipe);
或者,如果您实际上不需要捕获输出,则以下内容也完全避免了shell:
system( curl => ( '-sS',);
也就是说,我不知道你怎么可能发送包含NUL字节的字符串,这可能是一个gzip压缩文件.我认为你的做法本质上是有缺陷的.
你知道libcurl(卷曲的胆量)可以通过Net::Curl::Easy访问吗?