Sterh has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Any help appreciated . This works from console and even run by apache. but When I try to run it from web it shows :

malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "Can't connect to els...") at /usr/local/lib/perl5/site_perl/5.14.2/JSON.pm line 171.

my $client = REST::Client->new(); $client->addHeader('Authorization', 'Basic YWRtaW46cmFyaXRhbg=='); $client->addHeader('content_type', 'application/json'); $client->addHeader('accept', 'application/json'); $client->setHost('https://host'); $client->setTimeout(1000); $req = '/api/v2/devices?name_eq=193456'; $client->request('GET', qq($req)); $data1 = from_json($client->responseContent());
Thank you, Dmitry

Replies are listed 'Best First'.
Re: REST call from CGI do not work
by tobyink (Canon) on May 24, 2012 at 23:40 UTC

    $client->responseContent() is not returning JSON, so calling from_json on it results in an error.

    $client->responseContent() appears to be returning an error message. Try printing out that error message and reading it. It should give you a clue what to do next.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      You are right here is the print out of it

      Can't connect to 10.31.2.95:443 (certificate verify failed) LWP::Proto +col::https::Socket: SSL connect attempt failed with unknown error err +or:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate veri +fy failed at /usr/local/lib/perl5/site_perl/5.14.2/LWP/Protocol/http. +pm line 51.
Re: REST call from CGI do not work
by locked_user sundialsvc4 (Abbot) on May 25, 2012 at 12:01 UTC

    Stepping back away from this very familiar requirement a few paces, here are some things that worked for me:

    1. On the server side, use a package such as one of the RPC::Any family as a convenient framework and protocol for dealing with requests including any that result in a server-side error.   (Whether an error occurs or not, the server’s response should always be a JSON packet.   The suggested package-suite neatly deals with this any many other sundry matters.   Click on the link next to the author’s name to see everything in it.)
    2. On the client side, have the client include a short random string that the server is always obliged to echo back verbatim in its response, whatever that response may be.   The client side software checks it.
    3. As a design point, I find it convenient that, if the server returns an error response, the client side initially throws an exception of its own, and catches it elsewhere.

      Solution was to include this into the code:

       $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;

      So it will not check certificate

        Solution was to include this into the code:

        :) Ignoring a security problem is never a solution

        It also looks like you're using an older version of LWP toolchain

        The real solution is to upgrade LWP and all dependencies, even Mozilla::CA