Mojo::UserAgent doesn't work?

122 views
Skip to first unread message

Pavel Serikov

unread,
Jun 27, 2015, 5:44:15 AM6/27/15
to mojol...@googlegroups.com
Hello dear colleagues,

Can someone tell me why this script doesn't work?

#!/usr/bin/env perl
use Mojo::UserAgent;
use feature 'say';
use utf8;

my $ua = Mojo::UserAgent->new;
say $ua
->get('http://geocode-maps.yandex.ru/1.x/?geocode=Moscow&format=json')->res->json->{response}->{GeoObjectCollection}->{featureMember}->[0]->{GeoObject}->{metaDataProperty}->{GeocoderMetaData}->{AddressDetails}->{Country}->{CountryName};

When I run script I got such error:

pavel@U310T:~/projects/test_scripts$ ./test_geocode.pl
Can't use an undefined value as a HASH reference at ./test_geocode.pl line 8.

And seems like problem isn't in JSON structure or wrong Yandex API string. When I modified script to:

#!/usr/bin/env perl
use Mojo::UserAgent;
use Data::Dumper;
use utf8;
my $ua = Mojo::UserAgent->new;
warn
Dumper $ua->get('https://geocode-maps.yandex.ru/1.x/?geocode=Moscow&format=json')->res->json;

I got:

pavel@U310T:~/projects/test_scripts$ ./test_geocode.pl
$VAR1 = undef;

And as you can make sure API is working - https://geocode-maps.yandex.ru/1.x/?geocode=Moscow&format=json

My Mojo version is latest

sudo cpan -D Mojolicious
Loading internal null logger. Install Log::Log4perl for logging messages
Reading '/home/pavel/.cpan/Metadata'
  Database was generated on Sat, 27 Jun 2015 04:41:02 GMT
Mojolicious
-------------------------------------------------------------------------
    (no description)
    D/DB/DBOOK/Mojolicious-6.12.tar.gz
    /usr/local/share/perl/5.18.2/Mojolicious.pm
    Installed: 6.12
    CPAN:      6.12  up to date
    Sebastian Riedel (SRI)
    kra...@googlemail.com

I'm quite confused, cause recently this script worked... I didn't anything except updating Mojolicious to latest version.

Any ideas?

Sergey Andreev

unread,
Jun 27, 2015, 5:53:27 AM6/27/15
to mojol...@googlegroups.com

Did you got json from another app? Try curl or wget same url, for me your script return json.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Bernhard Graf

unread,
Jun 27, 2015, 5:59:51 AM6/27/15
to mojol...@googlegroups.com
You simply didn't get a JSON response. Try

warn
Dumper($ua->get('https://geocode-maps.yandex.ru/1.x/?geocode=Moscow&format=json')->res)


Pavel Serikov

unread,
Jun 27, 2015, 6:06:48 AM6/27/15
to mojol...@googlegroups.com
Hello Sergey.

Yes, I got json from Yandex Maps API.
With wget and curl it's working, but I need to realize it with UserAgent get() method.
I suppose that it's a bug of latest versions. Cause in 5.50 everything works ok. So let's wait maintainer's answer.

суббота, 27 июня 2015 г., 12:53:27 UTC+3 пользователь Sergey Andreev написал:

Pavel Serikov

unread,
Jun 27, 2015, 6:10:36 AM6/27/15
to mojol...@googlegroups.com
Yeah, it's working :)

But warn Dumper ($ua->get('http://geocode-maps.yandex.ru/1.x/?geocode=Москва&format=json')->res->json) doesn't. $VAR1 = undef

So problem is in res->json method?

суббота, 27 июня 2015 г., 12:59:51 UTC+3 пользователь Bernhard Graf написал:

Bernhard Graf

unread,
Jun 27, 2015, 6:59:23 AM6/27/15
to mojol...@googlegroups.com
> Yeah, it's working :)

What means "working"?
*What* do you get when you Dumper(...->res)?


Stefan Adams

unread,
Jun 27, 2015, 6:15:50 PM6/27/15
to mojolicious

On Sat, Jun 27, 2015 at 5:10 AM, Pavel Serikov <pavel.p...@gmail.com> wrote:
Yeah, it's working :)

But warn Dumper ($ua->get('http://geocode-maps.yandex.ru/1.x/?geocode=Москва&format=json')->res->json) doesn't. $VAR1 = undef

So problem is in res->json method?

Notice Bernhard's URL had https.  The http URL you supplied returns a 301 redirect to https, which of course the 301 response has no JSON in the response.

$ perl -MData::Dumper -MMojo::UserAgent -E 'warn Dumper (Mojo::UserAgent->new->get(q(https://geocode-maps.yandex.ru/1.x/?geocode=Moscow&format=json))->res->json)'
$VAR1 = {
          'response' => {
...
                      }
        };

Dan Book

unread,
Jun 27, 2015, 6:23:53 PM6/27/15
to mojol...@googlegroups.com

Pavel Serikov

unread,
Jul 15, 2015, 7:08:18 AM7/15/15
to mojol...@googlegroups.com
Thanks all for your suggestions.

Yeah, it first script I forgot to use https instead of http (or use $ua->max_redirects ).

And in second script updating Mojo from 6.12 to 6.14 was solved the problem :)


воскресенье, 28 июня 2015 г., 1:23:53 UTC+3 пользователь Dan Book написал:
Reply all
Reply to author
Forward
0 new messages