Search code examples
perlmojo-useragent

mojo::useragent and Get Request Parameters


Let's Say I have this Mojo::UserAgent get request :

use Mojo::UserAgent;
my $ua  = Mojo::UserAgent->new;
print $ua->get('https://google.com?q=mojolicious&format=json');

on the above example the get parameters provided as part of the url itself , I am asking if there are an option to separate the request parameters from the url

I tried form but its not achieving the same result like using this directly as url 'https://google.com?q=mojolicious&format=json'

print $ua->get('https://google.com' => form => {q= > 'mojolicious' ,format='json'});

any idea how to achieve the above ?


Solution

  • Your code has some formatting issues:

    • q= > 'mojolicious should read q => 'mojolicious'
    • format='json' should read format => 'json'
    • and you are missing a closing curly }

    So all in all your line should look like this:

    $ua->get('https://google.com' => form => {q => 'mojolicious', format => 'json' });
    

    This method returns an instance Mojo::Transaction::HTTP which you can use like this:

    my $tx = $ua->get('https://google.com' => form => { q => 'mojolicious', format => 'json' });
    
    print $tx->res->body;
    

    For further reading please consult https://metacpan.org/pod/Mojo::Transaction::HTTP