simone
simone

Reputation: 5233

Why does LWP::UserAgent succeed and Mojo::UserAgent fail?

If I make a request like this:

my $mojo_ua = Mojo::UserAgent->new->max_redirects(5);

$mojo_ua->inactivity_timeout(60)->connect_timeout(60)->request_timeout(60);;
$mojo_ua->transactor->name('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36');

my $headers = {
           'Accept' => 'application/json',
           'Accept-Language' => 'en-US,en;q=0.5',
           'Connection' => 'keep-alive',
           'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
           'x-csrf-token' => 'Fetch',
           'Accept-Encoding' => 'gzip, deflate, br',
           'DataServiceVersion' => '2.0',
           'MaxDataServiceVersion' => '2.0',
           'Referer' => 'https://blah.blas.com/someThing.someThing'
         };

my $url = Mojo::URL->new('https://blah.blah.com/irj/go/sap/FOO_BAR_BAZ/');

my $tx = $mojo_ua->get($url, $headers);
$tx = $mojo_ua->start($tx);
my $res = $tx->result;

the request times out, but if I take the exact same request, built in the same way and do this:

my $lwp_ua = LWP::UserAgent->new;

my $req = HTTP::Request->parse( $tx->req->to_string );
$req->uri("$url");
my $res = $lwp_ua->request($req);

it succeeds.

It happens in a few cases that Mojo::UserAgent fails, and LWP::UserAgent succeeds with exactly the same transaction, and I'm starting to get curious.

Any idea as to why?

Upvotes: 1

Views: 205

Answers (1)

Borodin
Borodin

Reputation: 126772

Your call to

$mojo_ua->get($url, $headers)

has already sent the HTTP request and received the response from the server, errored, or timed out. You don't need to call

$mojo_ua->start($tx)

as well, and that statement should be removed

If you really want to first build the transaction and then start it, you need

my $tx = $mojo_ua->build_tx(GET => $url, $headers);
$tx    = $mojo_ua->start($tx);

but I don't see any reason why you should need to do it this way

Upvotes: 3

Related Questions