i attempting download binary file hosted on github.com local disk. context requires in perl. here's test code:
use file::fetch; $fileurl = "https://github.com/libwww-perl/libwww-perl/archive/6.13.zip"; $to = "/tmp"; $ff = file::fetch->new(uri => "$fileurl") || die("fetch failed\n"); print stderr "fetch->new succeeded\n"; $where = $ff->fetch(to => $to); if ($where) { print stderr "fetched file '$where'\n"; } else { print stderr "fetch failed: " . $ff->error(1) . "\n"; exit(-1); }
this worked fine downloading test file non-github, mock-up web-server serving on http. file hosted on github, however, fails no illuminating error messages:
% ./trydownload fetch->new succeeded fetch failed: %
for github.com, above approach appears have 2 problems:
- it apparently not support https download offered github
- it apparently not follow redirect github seems use direct client actual storage location of file
internet searches turn veritable maze of components , pieces (i presume) construct working solution. not have days experiment getting these assembled properly, however.
does know cut-and-dried answer how make above code, or equivalent in perl, download binary file through https , apparent redirect? or @ least succeed @ download github.com?
here's same using libcurl wrapper www::curl::easy;
:
#!/usr/bin/perl use strict; use warnings; use www::curl::easy; use file::temp qw/tempfile/; ($out, $filename) = tempfile(); $curl = www::curl::easy->new(); $curl->setopt(curlopt_url, "https://github.com/libwww-perl/libwww-perl/archive/6.13.zip"); $curl->setopt(curlopt_writedata, $out); $curl->setopt(curlopt_verbose, 1); $curl->setopt(curlopt_followlocation, 1); $retcode = $curl->perform(); if ($retcode != 0) { print stderr "fetch failed: ", $curl->strerror($retcode), " ( +$retcode)\n"; print stderr "errbuf: ", $curl->errbuf; exit(1); } else { print stderr "fetched file to: ", $filename, "\n"; }