tanszek:oktatas:iss_t:json-rpc
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tanszek:oktatas:iss_t:json-rpc [2026/03/01 17:03] – knehez | tanszek:oktatas:iss_t:json-rpc [2026/03/01 17:27] (current) – [JSON-RPC Client (Python)] knehez | ||
|---|---|---|---|
| Line 24: | Line 24: | ||
| A JSON-RPC request is a JSON object with the following fields: | A JSON-RPC request is a JSON object with the following fields: | ||
| - | jsonrpc – must be " | + | * **jsonrpc** – must be " |
| - | + | * **method** – name of the remote method | |
| - | method – name of the remote method | + | * **params** – parameters (optional) |
| - | + | * **id** – request identifier (used to match response) | |
| - | params – parameters (optional) | + | |
| - | + | ||
| - | id – request identifier (used to match response) | + | |
| Example request: | Example request: | ||
| Line 43: | Line 40: | ||
| <code json> { " | <code json> { " | ||
| + | |||
| + | ==== Notifications ==== | ||
| + | |||
| + | If the client sends a request without an id, it becomes a notification. | ||
| + | |||
| + | The server must NOT send a response. | ||
| + | |||
| + | Example: | ||
| + | |||
| + | <code json> { " | ||
| + | |||
| + | ==== Batch Requests ==== | ||
| + | |||
| + | JSON-RPC supports sending multiple requests in one array: | ||
| + | |||
| + | <code json> [ {" | ||
| + | |||
| + | ==== 6. Practical Example in Python ==== | ||
| + | |||
| + | In this section, we implement a simple JSON-RPC server and client using Python. | ||
| + | |||
| + | We will use: | ||
| + | |||
| + | * Flask (for HTTP server) | ||
| + | |||
| + | * requests (for client) | ||
| + | |||
| + | Install dependencies: | ||
| + | |||
| + | <code bash> pip install flask requests </ | ||
| + | |||
| + | ===== JSON-RPC Server (Python + Flask) ===== | ||
| + | |||
| + | Create a file: // | ||
| + | |||
| + | <sxh python> | ||
| + | from flask import Flask, request, jsonify | ||
| + | |||
| + | app = Flask(__name__) | ||
| + | |||
| + | # available RPC methods | ||
| + | |||
| + | def add(a, b): | ||
| + | return a + b | ||
| + | |||
| + | |||
| + | def subtract(a, b): | ||
| + | return a - b | ||
| + | |||
| + | |||
| + | methods = { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | |||
| + | @app.route("/ | ||
| + | def rpc(): | ||
| + | data = request.get_json() | ||
| + | |||
| + | if data.get(" | ||
| + | return jsonify( | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ) | ||
| + | |||
| + | method_name = data.get(" | ||
| + | params = data.get(" | ||
| + | request_id = data.get(" | ||
| + | |||
| + | if method_name not in methods: | ||
| + | return jsonify( | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ) | ||
| + | |||
| + | try: | ||
| + | result = methods[method_name](*params) | ||
| + | return jsonify( | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ) | ||
| + | except Exception as e: | ||
| + | return jsonify( | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ) | ||
| + | |||
| + | |||
| + | if __name__ == " | ||
| + | app.run(port=5000) | ||
| + | |||
| + | </ | ||
| + | |||
| + | Run: | ||
| + | |||
| + | <code bash> python server.py </ | ||
| + | |||
| + | ===== JSON-RPC Client (Python) ===== | ||
| + | |||
| + | Create a file: // | ||
| + | |||
| + | <sxh python> | ||
| + | import requests import json | ||
| + | |||
| + | url = " | ||
| + | |||
| + | payload = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | response = requests.post(url, | ||
| + | |||
| + | print(" | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | Expected output: | ||
| + | |||
| + | < | ||
| + | |||
| + | ==== Error Codes in JSON-RPC 2.0 ==== | ||
| + | |||
| + | ^ Code ^ Meaning ^ | ||
| + | | -32700 | Parse error | | ||
| + | | -32600 | Invalid Request | | ||
| + | | -32601 | Method not found | | ||
| + | | -32602 | Invalid params | | ||
| + | | -32603 | Internal error | | ||
tanszek/oktatas/iss_t/json-rpc.1772384598.txt.gz · Last modified: 2026/03/01 17:03 by knehez
