Summary

This package implements a JSON-RPC 2.0 protocol to interface
with Foswiki and its plugins.

In contrast to the normal REST interface of Foswiki, a JSON-RPC interface
offers well defined calling semantics for requests and responses. The
interface will also take care that any received data is recoded
to the server's character encoding. JSON-RPC is normally called as part of
some JavaScript AJAX application.

JsonRpcContrib also comes with a jQuery plugin to simplify working with
JSON-RPC. This is a simple wrapper around jQuery's own
AJAX capabilities.

Registering JSON-RPC procedures

Foswiki plugins are able to register their own handler for a specific
method in a given namespace, thus:

Handler functions

The handler function in your plugin takes two parameters, $session and $request. $session is a reference to the Foswiki session; most implementers should simply ignore this. $request is a reference to the JSON request object. The following methods are available on this object:

param('param1') - returns the value of a single named parameter

params() - returns a reference to the entire parameter hash

method() - returns the method

namespace() - returns the namespace

The handler function can return a scalar or a reference to an acyclic graph (a tree structure). The structure may contain blessed data (perl objects) if (and only if) those objects implement the TO_JSON method described in the documentation for the CPAN JSON module.

Errors can be signalled using a simple die. Such errors will be returned to the caller with an errorCode of 1. If you need to pass back extended error information, you will have to encode it in the die message.

Calling using a POST

Once a handler is registered it may be called using an URL of the format:

https://wiki.digitalmethods.net/bin/jsonrpc/MyNamespace

... while POSTing a JSON-encoded request according to the JSON-RPC 2.0 specification,
like,

Success response

If the call is successful the JSON response will be of the format:

{
jsonrpc: "2.0",
result: some-result-object,
id: "caller's id"
}

Authentication

If there is an existing login session then JSON-RPC calls will be authenticated using that session. Alternatively, requests can be authenticated by passing in username and password URL parameters. It is strongly recommended that this is only done if the communications links is secure (https:), as these parameters are sent in plain text.

Extensions to the standard

JSON-RPC 2.0 normally only allows you to pass parameters to a remote
procedure using a well formed request object as described
above. However in real-live web applications, data to be transmitted to
a specific endpoint is most conveniently sent using URL parameters (as is
the case for normal HTML forms).

Instead of requiring all form fields to be converted into a
JSON-RPC request object on the client side, the JsonRpcContrib
converts form data to a proper request object transparently.
This way you can call a JSON-RPC function using a simple form submission
from the client.

The called namespace and method can thus be specified much like a
subject/verb url to a REST interface. These calls are equivalent:

Forms of this type can easily be sent to the server using JQueryForm's $.ajaxSubmit() method.

If a namespace, method, or parameters are specified as part of a JSON-RPC request object as well as using URL parameters, the URL parameters take higher precedence and are merged into the request object.

Installation Instructions

You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server.

Open configure, and open the "Extensions" section. Use "Find More Extensions" to get a list of available extensions. Select "Install".