我正在尝试使用特定的Web服务,我可以使用以下命令成功执行上载:
curl -X POST --header "Transfer-Encoding: chunked" -d @Downloads/file.pdf https://some.webservice/upload
我得到了一个json回复表明成功.
但是,我无法弄清楚如何对WWW :: Mechanize做同样的事情.
$mech->post("https://" . $server . "/upload",Content_Type => 'multipart/form-data',Content => [upID => $upid,name => $dlfile,userID => 0,userK => 0,file_0 => [$dlfile]]);
这会收到一个类似的json响应,其中包含一个很大的错误消息.
我是否需要先显式设置Transfer-Encoding标头?还有其他一些技巧吗?谷歌对此没有太多了解,Perlmonks也没有,而且文档有点迟钝.
解决方法
您可以使用HTTP :: Request :: StreamingUpload来完成
my $starttime = time(); my $req = HTTP::Request::StreamingUpload->new( POST => $url,path => $file,headers => HTTP::Headers->new( 'Transfer-Encoding' => 'chunked' ),); my $gen = $req->content; die unless ref($gen) eq "CODE"; my $total = 0; $req->content(sub { my $chunk = &$gen(); $total += length($chunk); print "\r$total / $size bytes (" . int($total/$size*100) . "%) sent," . int($total/1000/(time()-$starttime+1)) . " k / sec "; return $chunk; }); my $resp = $ua->request($req); print "\n"; unless ($resp->is_success) { die "Failed uploading the file: ",$resp->status_line; } my $con = $resp->content; return $con;