Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

REST::Client + Request Body

by ravigupta1 (Novice)
on Oct 13, 2020 at 07:56 UTC ( #11122762=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I am trying to query Quest Foglight REST API with below code (POST Method) but getting error:

malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "Not a SCALAR referen...")

FYI: My PERL code is already connected to FogLight REST API and collected the auth-token value in $mytoken.
Appreciate, if someone cold help me to understand cause of the error and how to resolve it.

FYI: I tried decoding the $body in JSON format but didn't helped.

my $jbody = JSON->new->utf8->decode($body);
Thanks in advance !! Regards, Ravi
$header ={ Auth-Token => $mytoken, Content-Type => "application/json", Accept => "application/json" }; $body ='{ "includes" : [ { "ids" : ["fbb7c736-852f-4158-aae3-1f97bc755cec"], "observationName" : "sql_batches" } ], "startTime" : 1602043200000, "endTime" : 1602129599000, "granularity" : 86400000, "numberOfValue" : 1, "retrievalType" : "RAW" }'; $client->POST('/topology/batchQuery',$body,$header);

Replies are listed 'Best First'.
Re: REST::Client + Request Body
by hippo (Chancellor) on Oct 13, 2020 at 08:32 UTC

    Here is an SSCCE which works perfectly for me using your provided "body" data.

    #!/usr/bin/env perl use strict; use warnings; use REST::Client; my $rest = REST::Client->new ({ host => 'http://httpbin.org' }); my $body ='{ "includes" : [ { "ids" : ["fbb7c736-852f-4158-aae3-1f97bc755cec"], "observationName" : "sql_batches" } ], "startTime" : 1602043200000, "endTime" : 1602129599000, "granularity" : 86400000, "numberOfValue" : 1, "retrievalType" : "RAW" }'; my $headers = { Accept => 'application/json', 'Content-type' => 'application/json', }; $rest->POST ('/anything', $body, $headers); print $rest->responseContent;

    You will see that I have quoted the Content-type key, perhaps you should too?


    🦛

      hippo++ but I think you sold yourself short by not pointing out the most important part of your SSCCE:

      print $rest->responseContent;
      Without knowing the what the REST API actually returned, it would be difficult for any monk here to help out. I would also suggest:
      print $rest->responseCode();
      and check the response code against the documentation. It would not be odd for an API to return a non-successful code with no body or a blank body -- which would be unparseable.

      My own approach to APIs is to always try doing it with curl first. Sure setting headers and json bodies with curl can be a bit daunting but using curl via the command line is a great way to explore and/or debug a REST api.

      -derby

      Our questioner should also quote the "Auth-Token" key and use strict; which would have caught that error. Alternately, Perl HTTP handling modules recognize Content_Type as meaning "Content-Type" and the underscore allows the fat comma autoquoting to work as intended.

        I don't see the value of making such a statement.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11122762]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-11-24 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?