Mojo::UserAgent::Role::Resume - Role for Mojo::UserAgent that provides resuming capability during downloads


use Mojo::UserAgent;

my $class = Mojo::UserAgent->with_roles('+Resume');

my $ua = $class->new(max_attempts => 5);

# works with blocking requests:
my $tx = $ua->get('');

# well as with non-blocking ones (including promises, etc):
$ua->get('', sub ($ua, $tx) {
    print $tx->res->headers->to_string;

# The last snippet will save the entire file to, and will print:
Connection: keep-alive
Content-Length: 103124119
Server: nginx
ETag: "48401320-6400000"
Last-Modified: Fri, 30 May 2008 14:45:52 GMT
Date: Thu, 27 Aug 2020 16:25:18 GMT
Content-Range: bytes 1733481-104857599/104857600
Content-Type: application/zip
Access-Control-Allow-Origin: *


Mojo::UserAgent::Role::Resume is a role for Mojo::UserAgent that allows the user-agent to retry a URL upon failure.

Retries are made after a connection error or after a server error (HTTP error 5xx) occurs.

It will intelligently determine whether the server it's downloading from properly supports ranged requests, and if it doesn't, then upon failure it will stop asking for a resume and request the complete file again from scratch.

It will request the original user-provided URL in its next attempts, not the one that may have resulted from redirections of the first attempt.

The $tx object returned is the last HTTP transaction that took place.


Mojo::UserAgent::Role::Resume adds the following attribute to the Mojo::UserAgent object:


my $ua = $class->new;

The number of attempts it will try (at most). What matters for each download is the value this attribute held at the time the first attempt took place.


  • Write tests
  • Check whether the module should also check whether "content received < total_size_based_on_headers" when determining whether to retry
  • Add events
  • Find the right minimum versions of its dependencies

Despite all of the above, this module works.


Copyright (C) Karelcom OÜ.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.