Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a binary file with HTTP::UserAgent

Tags:

http

raku

I'm trying to download a file using HTTP::UserAgent, but all my attempts thus far have ended with the following error.

Malformed UTF-8

I've tried to use the getstore sub, exported in the :simple subset.

getstore($upstream ~ %module<link>, $dist.absolute);

And using the HTTP::UserAgent class directly.

my $ua = HTTP::UserAgent.new;

$dist.spurt: $ua.get($upstream ~ %module<link>).content;

While looking through the source code of the module, I found a :bin argument for .get, so naturally I've tried that as well.

$dist.spurt: $ua.get($upstream ~ %module<link>, :bin).content;

But even with the :bin argument I get the Malformed UTF-8 error.

How would I go about downloading a binary file using HTTP::UserAgent?

EDIT: To give some more context, the $dist is just another IO::File object, created with tempdir.IO.add("dist.tar.gz"). I've also tried to .open this file and .spurt to the IO::Handle instead of the IO::File, but this returned the same error. Adding :bin to the $dist.open call does also not seem to have an effect.

with ($dist.open(:w)) {
    LEAVE { .close }
    .spurt: $ua.get($upstream ~ %module<link>, :bin).content;
}

The URL I'm working with in this particular case is https://cpan.metacpan.org/authors/id/V/VR/VRURG/Perl6/OO-Plugin-v0.0.5.tar.gz, but it doesn't seem to be limited to this URL only.

like image 656
Tyil Avatar asked Jan 17 '19 22:01

Tyil


1 Answers

try the :bin option for the spurt routine. This works for me (with http)

'/tmp/test.tar.gz'.IO.spurt( $ua.get('http://cpan.metacpan.org/authors/id/V/VR/VRURG/Perl6/OO-Plugin-v0.0.5.tar.gz').content, :bin);
like image 152
LuVa Avatar answered Nov 16 '22 13:11

LuVa