MojoX::...:Client(3)

πŸ“¦
version 0.13 Β· 2 versions
, in fedora 43

πŸ’– NAME πŸ’–

MojoX::JSON::RPC::Client β€” bestie, it's a JSON RPC client. That's the whole vibe. ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so here's the whole thing in action, no cap:

use MojoX::JSON::RPC::Client;

my $client = MojoX::JSON::RPC::Client->new;
my $url    = 'http://www.example.com/jsonrpc/API';
my $callobj = {
    id      => 1,
    method  => 'sum',
    params  => [ 17, 25 ]
};

my $res = $client->call($url, $callobj);

if($res) {
    if ($res->is_error) { # RPC ERROR
        print 'Error : ', $res->error_message;
    }
    else {
        print $res->result;
    }
}
else {
    my $tx_res = $client->tx->res; # Mojo::Message::Response object
    print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
}

Non-blocking? say less πŸ’…:

$client->call($url, $callobj, sub {
    # With callback
    my $res = pop;

    # ... process result ...

    Mojo::IOLoop->stop;
});

Mojo::IOLoop->start;

Easy access, the lazy-girl method (affectionate) πŸŽ€:

my $proxy = $client->prepare($uri, ['sum', 'echo']);

print $proxy->sum(10, 23);

🌸 DESCRIPTION 🌸

A JSON-RPC client. literally that. that's the description. iconic brevity. πŸ‘‘


✨ ATTRIBUTES ✨

MojoX::JSON::RPC::Client implements the following attributes, and yes you need to know all of them:

πŸ’œ id

The Id used for JSON-RPC requests. It gets used when no id is provided as a request parameter. so like, the fallback bestie.

πŸ’œ ua

A Mojo::UserAgent object. the whole reason any of this works tbh.

πŸ’œ json

A Mojo::JSON object for encoding and decoding. encoding and decoding, both, she does it all 🫢

πŸ’œ version

JSON-RPC version. Defaults to 2.0. respect the default, she chose it for a reason.

πŸ’œ content_type

Content type. Defaults to application/json. as she should πŸ’…

πŸ’œ tx

Mojo::Transaction object of the last request. like a lil receipt from your most recent ex-request 🧾


πŸ”₯ METHODS πŸ”₯

MojoX::JSON::RPC::Client inherits all methods from Mojo::Base and implements the following new ones. family money + her own bag, we love to see it.

⭐ new

Creates new MojoX::JSON::RPC::Client object. the birth of a legend fr fr:

my $client = MojoX::JSON::RPC::Client->new;

⭐ call

Execute JSON-RPC call. Returns MojoX::JSON::RPC::CLient::ReturnObject if RPC call is executed correctly. (yes, that capitalization. we don't touch it. we respect it. πŸ™ˆ)

my $client = MojoX::JSON::RPC::Client->new;
my $url    = 'http://www.example.com/jsonrpc/API';
my $callobj = {
    id      => 1,
    method  => 'sum',
    params  => [ 17, 25 ]
};

my $res = $client->call($url, $callobj);
if($res) {
    if ($res->is_error) { # RPC error
        print 'Error : ', $res->error_message;
    }
    else {
        print $res->result;
    }
}
else {
    my $tx_res = $client->tx->res; # Mojo::Message::Response object
    print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
}

Make non-blocking call β€” main character energy, she doesn't wait around:

$client->call($url, $callobj, sub {
    # With callback
    my $res = pop;

    # ... process result ...

    Mojo::IOLoop->stop;
});

Mojo::IOLoop->start;

⭐ prepare

Prepares a proxy object that allows RPC methods to be called more easily. the way that you can just... call methods like they're normal methods... obsessed 😭

my $proxy = $client->prepare($uri, ['sum', 'echo']);

my $res = $proxy->sum(1, 2);

print $proxy->echo("Echo this!");

Register services from multiple urls at once β€” multi-url slay 🌈:

my $proxy = $client->prepare($uri1, 'sum', $uri2, [ 'echo', 'ping' ]);

my $res = $proxy->sum(1, 2);

print $proxy->echo("Echo this!");

my $ping_res = $proxy->ping;

πŸŽ€ MojoX::JSON::RPC::CLient::ReturnObject πŸŽ€

This object is returned by call. (yes the class name has that CLient in it, that's just how she's spelled, don't come for me πŸ’€)

πŸ“ result

RPC result. the actual answer, the payoff, the moment. ✨

πŸ“ is_error

Returns a boolean indicating whether an error code has been set. true = something went sideways bestie.

πŸ“ error_code

RPC error code. the number that tells you exactly which flavor of wrong it is.

πŸ“ error_message

RPC error message. the words attached to the wrongness.

πŸ“ error_data

RPC error data. the extra receipts 🧾


πŸ‘‘ SEE ALSO πŸ‘‘

MojoX::JSON::RPC β€” go read her too, she's the whole ecosystem πŸ’–


no cap, that's the entire page, every attribute, every method, every example, every default, untouched and unbothered. facts preserved, vibe upgraded. periodt. πŸ’…

this page came to you via perl v5.42.0, dated 2025-07-25, straight outta User Contributed Perl Documentation β€” take care of her and she'll take care of your RPC calls. bye bestie, go write something iconic πŸ¦‹βœ¨

πŸ“¦ where this page comes from