JSON::...pache2(3pm)

πŸ’– JSON::RPC::Legacy::Server::Apache2 β€” a.k.a. JSON::...pache2(3pm) πŸ’–

okay bestie so real quick: this page comes to you straight from the User Contributed Perl Documentation fam, and today we're talking about the JSON-RPC server for mod_perl2. yes. that one. let's get into it 🫢

✨ NAME ✨

JSON::RPC::Legacy::Server::Apache2 β€” the JSON-RPC sever for mod_perl2 (their spelling, not mine, love them for it πŸ’…)

🌸 SYNOPSIS 🌸

so here's the whole vibe. first, in apache conf:

# In apache conf

PerlRequire /your/path/start.pl
PerlModule MyApp

<Location /jsonrpc/API>
     SetHandler perl-script
     PerlResponseHandler JSON::RPC::Legacy::Server::Apache
     PerlSetVar dispatch "MyApp"
     PerlSetVar return_die_message 0
</Location>

and then in your application class:

#--------------------------
# In your application class
package MyApp;

use base qw(JSON::RPC::Legacy::Procedure); # Perl 5.6 or more than

sub echo : Public {    # new version style. called by clients
    # first argument is JSON::RPC::Legacy::Server object.
    return $_[1];
}

sub sum : Public(a:num, b:num) { # sets value into object member a, b.
    my ($s, $obj) = @_;
    # return a scalar value or a hashref or an arryaref.
    return $obj->{a} + $obj->{b};
}

sub a_private_method : Private {
    # ... can't be called by client
}

sub sum_old_style {  # old version style. taken as Public
    my ($s, @arg) = @_;
   return $arg[0] + $arg[1];
}

like, that's literally it. you write the subs, the module handles the rest. icon behaviour honestly πŸ‘‘

πŸ¦‹ DESCRIPTION πŸ¦‹

k so what actually happens under the hood, step by step, no gatekeeping:

  • Gets a client request.
  • Parses its JSON data.
  • Passes the server object and the object decoded from the JSON data to your procedure (method).
  • Takes your returned value (scalar or arrayref or hashref).
  • Sends a response.

and that's the whole loop bestie. Well, you write your procedure code only. the module carries everything else on its back πŸ’ͺ

πŸŽ€ METHODS πŸŽ€

They are inherited from the JSON::RPC::Legacy::Server methods basically. The below methods are implemented in JSON::RPC::Legacy::Server::Apache2.

  • new β€” Creates new JSON::RPC::Legacy::Server::Apache2 object. fresh out the box, slay ✨
  • handle β€” Runs server object and returns a response.
  • retrieve_json_from_post β€” retrieves a JSON request from the body in POST method.
  • retrieve_json_from_get β€” In the protocol v1.1, 'GET' request method is also allowable. it retrieves a JSON request from the query string in GET method.
  • response β€” returns a response JSON data to a client.

🧚 SEE ALSO 🧚

go read these next, i'm obsessed with all of them fr fr:

πŸ’œ AUTHOR πŸ’œ

Makamaka Hannyaharamitu, <makamaka[at]cpan.org> β€” we stan 🫢

Copyright 2007-2008 by Makamaka Hannyaharamitu

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


ngl the legal bit is short and sweet, but please actually read it before you go redistributing anything, okay? πŸ’–

and that's a wrap, bestie!! 🌈 sending you off with the source/version deets as a little parting gift: this was perl v5.42.0, dated 2015-08-27 πŸŽ€βœ¨ bye pookie, go write some Public subs πŸ’…πŸ”₯

πŸ“¦ where this page comes from