Generating requests

The request function

Generate a request with the request function:

>>> from jsonrpcclient import request
>>> request("ping")
{"jsonrpc": "2.0", "method": "ping", "2.0", "id": 1}


Subsequent calls increment the id:

>>> request("ping")
{"jsonrpc": "2.0", "method": "ping", "2.0", "id": 2}
>>> request("ping")
{"jsonrpc": "2.0", "method": "ping", "2.0", "id": 3}

Use an explicit id:

>>> request("ping", id="foo")
{"jsonrpc": "2.0", "method": "ping", "2.0", "id": "foo"}

Or generate a different type of id:

>>> from jsonrpcclient import request_hex, request_random, request_uuid
>>> request_hex("foo")
{"jsonrpc": "2.0", "method": "foo", "id": "1"}
>>> request_random("foo")
{"jsonrpc": "2.0", "method": "foo", "id": "qzsib147"}
>>> request_uuid("foo")
{"jsonrpc": "2.0", "method": "foo", "id": "45480a2f-069c-42aa-a67f-f6fdd83d6026"}


Pass params to include parameters in the payload. This should be either a tuple for positional arguments, or dict for keyword arguments.

>>> request("ping", params=(1,))
{"jsonrpc": "2.0", "method": "ping", "2.0", "params": [1], "id": 4}
>>> request("ping", params={"key": "val"})
{"jsonrpc": "2.0", "method": "ping", "2.0", "params": {"key": "val"}, "id": 5}

JSON requests

If you need the request serialized to a string, use request_json:

>>> from jsonrpcclient import request_json
>>> request_json("foo")
'{"jsonrpc": "2.0", "method": "foo", "id": 6}'

You can also use request_json_hex etc., for the other id types.

Batch requests

>>> import json
>>> json.dumps([request("foo") for _ in range(3)])
'[{"jsonrpc": "2.0", "method": "foo", "id": 7}, {"jsonrpc": "2.0", "method": "foo", "id": 8}, {"jsonrpc": "2.0", "method": "foo", "id": 9}]'


Use the notification function instead of request:

>>> from jsonrpcclient import notification
>>> notification("ping")
{"jsonrpc": "2.0", "method": "ping"}

Similar to request_json, notification_json will give you the notification as a JSON string.

>>> from jsonrpcclient import notification_json
>>> notification_json("ping")
'{"jsonrpc": "2.0", "method": "ping"}'