1

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 ?

dave
  • 867
  • 1
  • 5
  • 11

1 Answers1

3

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

PiFanatic
  • 233
  • 1
  • 4
  • 14
  • Maybe more usefully read https://metacpan.org/pod/Mojo::UserAgent::Transactor#tx — which is where it's documented that yes, `form` *does* work and generate query params for `GET`/`HEAD` requests, along with many handy examples. – hobbs Aug 05 '22 at 21:22