jsonrpc API reference
JSON-RPC 2.0 (https://www.jsonrpc.org/specification) over HTTP: a client that calls remote methods, and a transport-agnostic server handle that dispatches an incoming request to the entry program's functions by name, via meta.callMain - the method name arrives on the wire, so it is resolved as a runtime string, not a func value. Built on json for the wire format and http for the client transport, so it needs the default jennifer binary.
params and a call's result are json.Values: the caller builds params with the json write API (json.list / json.map + json.append / json.set) and reads the result with the json accessors. Any client-side failure - a JSON-RPC error reply, a transport error (malformed HTTP), or a malformed reply - surfaces as a single catchable jsonrpc.RpcError (kind "jsonrpc"), whose reason, code and data say which. The generic kinds pass as they are: a socket failure (refused, reset, DNS) stays http's net.NetError (kind "net"), a reached cap kind "limit", a deadline "timeout", a reply cut short "closed", a cancelled task "cancelled". A client carrying an Authorization header refuses an http:// endpoint on a non-loopback host before connecting, unless its allowInsecureHttp field is set (plain HTTP on a trusted LAN only).
Server security. handle dispatches each request's method to a top-level func of that name in the entry program, so every top-level function taking one json.Value parameter is remotely reachable - there is no separate route registry. Name RPC handlers deliberately (a shared prefix, a dedicated dispatch file) and do not co-locate a handle-served program with privileged one-argument helpers. Authentication is the transport's job: gate on a header / token before calling handle. A handler that throws yields a generic -32603 reply (the thrown message is not put on the wire), so raise errors freely without leaking internals.
Import with import "jsonrpc.j" as jsonrpc;. See the jsonrpc guide for prose and examples.
Contents
| Name | What it is |
|---|---|
call | Call a remote method and return its result. |
client | Build a client for a JSON-RPC endpoint. |
clientWith | Build a client with extra request headers (e.g. |
handle | Dispatch a JSON-RPC request body and return the reply body (transport- agnostic: wire it to httpd / net... |
notify | Send a notification: a request with no id, for which the server returns no reply. |
parseReply | Read a JSON-RPC 2.0 response body the way call does, for a transport other than this module's own HTTP PO... |
Client | struct: A client bound to a JSON-RPC HTTP endpoint. |
RpcError | struct: A failed call or notification. |
| constants | the module's constants |
Functions
call
jsonrpc.call(client as Client, method as string, params as json.Value)Call a remote method and return its result. Any failure - a JSON-RPC error reply, a transport error (malformed HTTP), or a reply that is not a JSON-RPC 2.0 response (jsonrpc not "2.0", neither or both of result and error, an error that is not an object with an int code and a string message, or an id that does not match the request) - throws a catchable RpcError (kind "jsonrpc"); a socket failure is a net.NetError (kind "net"), a reached cap kind "limit", a deadline "timeout", a reply cut short "closed".
client- the endpoint client
method- the method name
params- the params (a
json.listarray or ajson.mapobject; passjson.list()for a method that takes no arguments) - Returns
json.Value - the
resultmember of the reply
client
jsonrpc.client(endpoint as string)Build a client for a JSON-RPC endpoint.
endpoint- the endpoint URL
- Returns
Client - a configured client
clientWith
jsonrpc.clientWith(endpoint as string, headers as map of string to string)Build a client with extra request headers (e.g. Authorization).
endpoint- the endpoint URL
headers- headers sent with every request
- Returns
Client - a configured client
handle
jsonrpc.handle(requestBody as string)Dispatch a JSON-RPC request body and return the reply body (transport- agnostic: wire it to httpd / net however you serve). Each request's method names a top-level function func NAME(params as json.Value) in the program that imported this module; it is called with the request's params and must return a json.Value or a scalar (int / float / string / bool / null) as its result. A missing function is a -32601 reply, a thrown error a generic -32603 reply. A request that is not a JSON-RPC 2.0 request object (not an object, a jsonrpc other than "2.0", a method that is not a string, params that are not an array or an object, an id that is not a string, a number or null) is a -32600 reply, with a null id when the id itself is the problem. A single request yields a single reply; a notification (no id) yields no reply (""); a batch (a JSON array) yields an array reply with the notification entries omitted ("" when the batch is all notifications). A batch of more than 1000 requests is refused with one -32600 reply.
Security. Every top-level one-json.Value-argument function is reachable by name (there is no route allow-list); the module-level doc explains how to scope handlers and where authentication belongs. A thrown handler error's message is not echoed to the client (only the generic -32603 text is), so internal detail does not leak.
requestBody- the raw request JSON
- Returns
string - the reply JSON, or
""when no reply is owed
notify
jsonrpc.notify(client as Client, method as string, params as json.Value)Send a notification: a request with no id, for which the server returns no reply. A delivered notification says nothing about whether the method ran, but a failed request still throws as call does: a deadline is kind "timeout", and a socket failure (refused, DNS) a net.NetError (kind "net"), which means the request never reached the server. A non-2xx answer is an RpcError: reason "rpc" when its body is a JSON-RPC error object, else "http".
client- the endpoint client
method- the method name
params- the params (see
call)
parseReply
jsonrpc.parseReply(body as string, status as int, id as int)Read a JSON-RPC 2.0 response body the way call does, for a transport other than this module's own HTTP POST (such as mcp's, which may carry the response in an event stream).
body- the response JSON
status- the HTTP status it came with (0 for none), carried into an error
id- the id of the request it answers
- Returns
json.Value - the
resultmember - Throws
RpcError - kind "jsonrpc": reason "rpc" for an error object, "protocol" for a body that is not a JSON-RPC 2.0 response to
id(kind "limit" for one pastjson's decoding caps)
Structs
Client
A client bound to a JSON-RPC HTTP endpoint. Value-semantic; build with client / clientWith.
def struct Client {
endpoint as string,
headers as map of string to string,
allowInsecureHttp as bool
};endpoint- the JSON-RPC endpoint URL (
http://orhttps://) headers- extra request headers (auth, ...)
allowInsecureHttp- allow an
Authorizationheader over plainhttp://to a non-loopback host (default false)
RpcError
A failed call or notification. Every error of kind "jsonrpc" is one, so its fields are safe to read once the kind matches.
def struct RpcError {
kind as string,
message as string,
reason as string,
status as int,
code as int,
data as json.Value
};kind- always
"jsonrpc" messagejsonrpc error <code>: <message>for an error reply, else what went wrongreason"rpc"(the server answered with an error object),"transport"(a malformed HTTP answer),"protocol"(an answer that is not a JSON-RPC 2.0 response object: not JSON, ajsonrpcother than"2.0", neither or both of result and error, a malformed error object, or another id),"http"(a notification answered with a non-2xx status) or"insecure"(anAuthorizationheader refused over plainhttp://; nothing was sent)status- the HTTP status of the answer; 0 for
"transport"and"insecure" code- the error object's
code(such as -32601, method not found); 0 unlessreasonis"rpc"or the object had none data- the error object's
datamember; JSONnullwhen it has none orreasonis not"rpc"
Constants
jsonrpc.INTERNAL_ERROR as int- Internal JSON-RPC error (-32603); also what a throwing handler is answered with.
jsonrpc.INVALID_PARAMS as int- Invalid method parameters (-32602).
jsonrpc.INVALID_REQUEST as int- The JSON sent is not a valid request object (-32600).
jsonrpc.METHOD_NOT_FOUND as int- The method does not exist or is not available (-32601).
jsonrpc.PARSE_ERROR as int- Invalid JSON was received (-32700).