JSON-RPC Client
jsonrpcclient is a Python library for generating JSON-RPC requests and parsing responses according to the JSON-RPC 2.0 specification. It is designed to be transport-agnostic, focusing solely on the protocol messaging rather than the underlying communication method. The current version is 4.0.3, released on February 23, 2023, and the library appears to be actively maintained with periodic updates.
Common errors
-
ModuleNotFoundError: No module named 'jsonrpcclient.clients'
cause Attempting to import from the `jsonrpcclient.clients` module, which was removed in version 4.0.0.fixUpgrade your code to the v4.x API, which uses `from jsonrpcclient import request, parse`. If you need client-like functionality, handle the transport manually. Alternatively, downgrade to a v3.x release (e.g., `pip install jsonrpcclient==3.3.6`). -
TypeError: Object of type Request is not JSON serializable
cause You are passing the Python object returned by `jsonrpcclient.request()` directly to a JSON serialization function (e.g., `json.dumps`) or a transport library expecting a raw JSON string, without it being a serializable dictionary.fixEnsure that if `jsonrpcclient.request()` is used, its output (a dictionary-like object) is passed to a serialization function like `json.dumps()` or directly to a library that handles dictionary-to-JSON serialization (e.g., `requests.post(url, json=my_dict)`). Alternatively, use `jsonrpcclient.request_json()` to get a pre-serialized JSON string. -
Server returned: {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": 1}cause The method name provided in the JSON-RPC request does not match any method exposed by the target server.fixVerify the exact method name, including case sensitivity, against the server's API documentation. Ensure there are no typos. -
Server returned: {"jsonrpc": "2.0", "error": {"code": -32602, "message": "Invalid params"}, "id": 1}cause The parameters sent in the request do not match the expected type, number, or structure (e.g., positional vs. named arguments) for the specified method on the server.fixConsult the server's API documentation to confirm the correct parameter types, order, and whether positional (`[value1, value2]`) or named (`{"key1": value1}`) parameters are expected. Adjust your `params` accordingly.
Warnings
- breaking Version 4.0.0 introduced significant breaking API changes. Modules like `jsonrpcclient.clients` and `jsonrpcclient.exceptions` were removed. Code written for `jsonrpcclient` v3.x and earlier is not compatible with v4.x without migration.
- gotcha `jsonrpcclient` is a protocol library and does not handle network transport (e.g., HTTP, WebSockets) itself. Users must integrate a separate library (like `requests` or `websockets`) for sending and receiving messages over the network.
- gotcha Distinguish between `request()`/`parse()` (for Python dictionary-like objects) and `request_json()`/`parse_json()` (for raw JSON strings). Misusing these can lead to serialization errors or incorrectly formatted messages.
Install
-
pip install jsonrpcclient
Imports
- request
from jsonrpcclient.clients.http import HTTPClient
from jsonrpcclient import request
- parse
from jsonrpcclient import parse
- request_json
from jsonrpcclient import request; request('method', params=...) # for JSON string outputfrom jsonrpcclient import request_json
Quickstart
import requests
import json
from jsonrpcclient import request, parse
# Define a mock JSON-RPC server URL for demonstration. Replace with your actual endpoint.
# For a real server, ensure it's running and accessible.
# Example: A simple server might expose a 'subtract' method.
jsonrpc_server_url = "http://localhost:5000/jsonrpc"
# 1. Create a JSON-RPC request (Python dictionary-like object)
# This example requests the 'subtract' method with positional parameters and an ID.
request_obj = request("subtract", params=[42, 23], id=1)
print(f"Generated JSON-RPC Request: {json.dumps(request_obj, indent=2)}")
# 2. Send the request using a transport layer (e.g., 'requests' for HTTP)
try:
# In a real application, you would send request_obj to your server
# and get a response_data from it. Example using 'requests' library:
# response_obj_from_server = requests.post(jsonrpc_server_url, json=request_obj).json()
# For quickstart, simulate a successful response from a server
response_obj_from_server = {"jsonrpc": "2.0", "result": 19, "id": 1}
print(f"\nReceived JSON-RPC Response Object: {json.dumps(response_obj_from_server, indent=2)}")
# 3. Parse the received response
parsed_response = parse(response_obj_from_server)
# 4. Handle the parsed response
if parsed_response.ok:
print(f"\nSuccessfully Parsed Result: {parsed_response.result}")
else:
print(f"\nError Received from Server (Code: {parsed_response.error.code}): {parsed_response.error.message}")
if parsed_response.error.data:
print(f" Error Data: {parsed_response.error.data}")
except requests.exceptions.ConnectionError:
print(f"\nError: Could not connect to the JSON-RPC server at {jsonrpc_server_url}. Please ensure the server is running.")
except json.JSONDecodeError:
print("\nError: Failed to decode JSON response from the server.")
except Exception as e:
print(f"\nAn unexpected error occurred: {e}")