WWW::Curl::Easyでファイルをアップロード
目的
WWW::Curl::EasyでファイルをPOSTでアップロードしたい。 ただし、ファイルは巨大になるケースを想定して、一旦メモリに読み込むのを避けたい。
コード
use strict; use warnings; use WWW::Curl::Easy; my $file = 'hoge.csv'; my $size = -s $file; open my $fh, '<', $file or die 'Cannot open file'; my $url = 'http://example.com/sample'; my @headers = ( "Content-type: text/csv; charset=utf8" ); my $curl = WWW::Curl::Easy->new(); $curl->setopt(CURLOPT_POST, 1); $curl->setopt(CURLOPT_URL, $url); $curl->setopt(CURLOPT_POSTFIELDSIZE_LARGE, $size); $curl->setopt(CURLOPT_READDATA, \$fh); $curl->setopt(CURLOPT_HTTPHEADER, \@headers); if ( $curl->perform != 0) { die 'fail to upload'; } my $code = $curl->getinfo(CURLINFO_HTTP_CODE); print "Code: $code"; close $fh;
参考
https://stackoverflow.com/questions/9485157/wwwcurl-how-to-upload-post-large-files