Problems decoding the response body when calling a REST service with Mojolicious for Perl

79 Views Asked by At

I am trying to learn how to use the Mojolicious framework for Perl. I am attempting to register a callback URL with a REST service and save the RequestID returned by the REST service. I successfully get back the request but can't decode the individual values that are part of the hash.

I have the following code:

use Mojo::UserAgent;
use Mojo::Message::Response;
use Mojo::JSON;
use Mojo::Promise;
use Mojo::Content::MultiPart;
my $requestURL = "http://localhost:31297/api/CallBack/RegisterCallBackURL?";
$requestURL = $requestURL."CallBackURL=http://localhost:8081/status";
$requestURL = $requestURL."&VerboseFlag=false";
$requestURL = $requestURL."&api-version=1.2";
my $Acknowledged = "";
my $VerboseInfo = "";
my $CallBackURL = "";
my $RequestID = "";
my $CurrentJobStatus = "";
my $TimeStampReceived = "";
my $ua = Mojo::UserAgent->new;
my $tx = $ua->post($requestURL,
                form => {
                "Acknowledged" => $Acknowledged,
                "VerboseInfo" => $VerboseInfo,
        "CallBackURL" => $CallBackURL,
            "RequestID" => $RequestID,
             "CurrentJobStatus" => $CurrentJobStatus,
             "TimeStampReceived" => $TimeStampReceived
                   });
my $res = $tx->result;                  
if ($res->is_success) 
{
    print "success";
    print $res->message;
    print $res->body;
    print $RequestID;
}
else
{
    print "Error";
    print $res->message;
}

I get back the following response, but the individual variables do not contain data.

Process started (PID=5440) >>>
successOK{"Acknowledged":false,"VerboseInfo":false,"CallBackURL":"http://localhost:8081/status","RequestID":"98d8df79-6897-4716-8084-ee756d16a1bf","CurrentJobStatus":null,"TimeStampReceived":"2023-06-29T04:44:03.2440429-07:00"}<<< Process finished (PID=5440). (Exit code 0)
================ READY ================
2

There are 2 best solutions below

0
Dave Cross On

You have received a JSON response (which you can see in $res->body). You need to decode that response (using Mojo::JSON) and then extract the individual data items into your variables.

0
brian d foy On

You've got a lot going on there, most of which you can simply. Most of the time, you don't need to load any modules that you don't explicitly use. Mojolicious is going to load everything it needs and you just follow the interface.

Maybe you have some sort of weird app that treats URL params differently from body params, but for many cases that should go together.

After that, you have a JSON response. Use the json response method to turn that into a Perl data structure:

use Mojo::UserAgent;

my $url = "http://localhost:31297/api/CallBack/RegisterCallBackURL";
my $form = {
    CallBackURL => 'http://localhost:8081/status',
    ...
    Acknowledged => $Acknowledged,
    ...
    };

my $tx = $ua->post( $url => form => $form );
if( $tx->res->is_success ) {
    my $data = $tx->res->json;
    ... now do stuff with data ...
    }

But let's back up to the query params in the URL. That's still really weird (but exists some places). If you want to do that and you are already using Mojolicious, use Mojo::URL to put it together:

use Mojo::URL;

my $base = 'http://localhost:31297/api/CallBack/RegisterCallBackURL';
my $url = Mojo::URL->new( $base )->query(
    CallBackURL   => 'http://localhost:8081/status',
    VerboseFlag   => 'false',
    'api-version' => '1.2',
    );

print $url;

I have a lot more examples in my book Mojolicious Web UserAgents.