lcikgl lcikgl - 1 year ago 142
Perl Question

Perl download of binary file from (github) URL with HTTPS and Redirect

I am attempting to download to local disk a binary file hosted on The context requires I do this in Perl. Here's my test code:

use File::Fetch;

$fileurl = "";
$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 to '$where'\n";
} else {
print STDERR "Fetch failed: " . $ff->error( 1 ) . "\n";
exit( -1 );

This worked fine downloading a test file from my non-github, mock-up web-server serving over HTTP. For the file hosted on github, however, it fails with no illuminating error messages:

% ./trydownload
Fetch->new succeeded
Fetch failed:

For, the above approach appears to have two problems:

  1. It apparently does not support the HTTPS download offered by github

  2. It apparently does not follow the redirect github seems to use to direct the client to the actual storage location of the file

Internet searches turn up a veritable maze of components and pieces with which I could (I presume) eventually construct a working solution. I do not have days to experiment with getting all these assembled properly, however.

Does someone know the cut-and-dried answer to how to make the above code, or equivalent in Perl, download a binary file through HTTPS and an apparent redirect? Or at least succeed at the download from

Answer Source

Here's the same using the libcurl wrapper WWW::Curl::Easy;

use strict;
use warnings;
use WWW::Curl::Easy;
use File::Temp qw/tempfile/;

my ($out, $filename) = tempfile();

my $curl = WWW::Curl::Easy->new();
$curl->setopt(CURLOPT_URL, "");
my $retcode = $curl->perform();

if ($retcode != 0) {
  print STDERR  "Fetch Failed: ", $curl->strerror($retcode), " ( +$retcode)\n";
  print STDERR  "errbuf: ", $curl->errbuf;
} else {
  print STDERR  "Fetched file to: ", $filename, "\n";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download