Reputation: 43
When I try to run script on my second computer I get this message:
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "LWP will support htt...") at iptest.pl line 21, line 2.
On my first computer, the script works fine.
Line 21:
my $data = decode_json($resp->content);
Does anyone know what the problem can be?
Thanks in advance
Upvotes: 0
Views: 1640
Reputation: 1
I got in the same situation. After I used 'yum' to install perl, I still got the error when run the perl script.
$ sudo yum install perl
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management
...
Eventually I did these, and it works.
$ lwp-request https://google.com/
It returned an error message .. (LWP::Protocol::https not installed)
$ sudo rpm -ivh ~/perl-LWP-Protocol-https-6.07-4.el8.noarch.rpm
$ lwp-request https://google.com/
it returned a HTML page.
and my perl script can run without error.
(my system is:
$ cat /etc/os-release PRETTY_NAME=Red Hat Enterprise Linux 8.0 )
Upvotes: 0
Reputation: 121
For the record, this fixed the issue for me (CentOS):
# yum install perl-Crypt-SSLeay
Upvotes: 1
Reputation: 186
I applied most of the changes ikegami suggested. Then perl gave me good error messages to fix the remaining issues. It looks like it works now. Don't ask why it didn't work before. Your code is weird that it's hard to say what exactly went wrong. With strict and warnings you're forced to write better code. Maybe add some nicely named subroutines to add more clarity.
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use LWP::UserAgent;
use open qw(:std :utf8);
use LWP::Simple;
use YAML::Tiny;
use JSON;
use URI;
use List::MoreUtils qw(uniq);
print "Enter Qve:";
my ( $qve, $loc, $key, $href );
chomp( $qve = <STDIN> );
print "Enter Location:";
chomp( $loc = <STDIN> );
$key = '';
my $format = '$format';
$href =
"https://api.datamarket.azure.com/Bing/Search/v1/Web?Query='$qve [loc:$loc]'&Latitude=43&Longitude=19&$format=JSON";
my $ua = LWP::UserAgent->new('keep_alive');
$ua->credentials( "api.datamarket.azure.com" . ':443', '', '', $key );
my $resp = $ua->get($href);
my $data = decode_json( $resp->decoded_content( charset => 'none' ) );
my @urls = map { $_->{'Url'} } @{ $data->{d}->{results} };
my @za;
for my $i ( 0 .. $#urls ) {
my $trz = "www.";
my $host = URI->new( $urls[$i] )->host;
$host =~ s/$trz//g;
push( @za, $host );
}
Upvotes: 1
Reputation: 47849
I'm a bit surprised that the JSON error is the only error you get. But it does contain a tiny little hint: "LWP will support htt...". I bet that LWP is missing a module it needs to be able to make https connections. You now have two options:
$response->content
to see the full error message.lwp-request https://google.com/
. You should see the full error message. Then install the missing module.
And of course: please, please, please:
use strict
and use warnings
use
-line you don't need: IO::Socket, LWP::Simple, YAML::Tiny.LWP::UserAgent->new(keep_alive)
? Hint: It won't help to quote keep_alive
.Upvotes: 3
Reputation: 385915
Some issues:
use strict; use warnings;
.$response->content
. What it returns is useless. Instead, use $response->decoded_content( charset => 'none')
.chomp
the values you get from STDIN.our
unless forced to (e.g. our @ISA
= ok). my
should be used instead.my $format = '$format'; "$format"
is a silly way of writing "\$format"
.Upvotes: 2