My goal is to:
- create a valid transaction by hand with Mojo::UserAgent
- once this works, dump the request to a text file using
$tx->req->to_string - then re-use the transaction by creating a new one via parsing the file's contents
The re-use program looks like this:
use Mojo::Util qw/dumper/;
use Mojo::File qw/path/;
use Mojo::UserAgent;
use Mojo::Message::Request;
use Mojo::Transaction::HTTP;
my $req_string = path($ARGV[0])->slurp;
my $ua = Mojo::UserAgent->new;
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->parse($req_string);
print dumper $tx->req; # seems to print a valid Mojo::Message::Request object
$tx = $ua->start($tx);
# this fails with Can't call method "server" on an undefined value at /home/me/perl5/perlbrew/perls/perl-5.30.2/lib/site_perl/5.30.2/Mojo/Server/Daemon.pm line 55.
print dumper $tx->res->body;
which fails at line 16 with
Can't call method "server" on an undefined value at /home/me/perl5/perlbrew/perls/perl-5.30.2/lib/site_perl/5.30.2/Mojo/Server/Daemon.pm line 55.
In general, this seems to fail:
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $tx = Mojo::Transaction::HTTP->new;
# more transaction defining stuff
$tx = $ua->start($tx);
print $tx->res->body; # success!
my $txt = $tx->req->to_string;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->parse($txt);
$tx = $ua->start($tx); # failure!
print $tx->res->body;
What am I doing wrong?
I have no idea whether this is a bug or not. This seems to fix the problem:
Why does this help?
If you added Carp::Always, you'd see that the error comes from line 317 in Mojo::UserAgent:
How comes the $url is not absolute? Becuase in Mojo::URL,
is_absis defined asAnd if we inspect the url coming from the request, it indeed doesn't have a scheme:
You can see the difference in the Data::Dumper output:
That's also why the hack above fixes the problem: it sets the missing scheme (and host) in the second URL.
Let's now wait for someone more Mojo-savvy to explain all this to us.