### JSON-RPC Batch Call Example Source: https://www.jsonrpc.org/specification/index Provides a comprehensive example of a JSON-RPC batch request containing a mix of valid calls, notifications, and invalid requests. It shows the corresponding responses, including results for valid calls and errors for invalid ones. ```json --> [ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, {"foo": "boo"}, {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ] <-- [ {"jsonrpc": "2.0", "result": 7, "id": "1"}, {"jsonrpc": "2.0", "result": 19, "id": "2"}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"}, {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} ] ``` -------------------------------- ### JSON-RPC Notification Examples Source: https://www.jsonrpc.org/specification/index Shows examples of JSON-RPC notifications, which are requests that do not expect a response. These are typically used for fire-and-forget operations or to signal events. ```json --> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]} --> {"jsonrpc": "2.0", "method": "foobar"} ``` -------------------------------- ### JSON-RPC Call with Named Parameters Source: https://www.jsonrpc.org/specification/index Illustrates a JSON-RPC call utilizing named parameters for the 'subtract' method. This example showcases how to pass parameters as an object, allowing for flexible ordering and improved readability. ```json --> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3} <-- {"jsonrpc": "2.0", "result": 19, "id": 3} --> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4} <-- {"jsonrpc": "2.0", "result": 19, "id": 4} ``` -------------------------------- ### JSON-RPC 2.0 Batch Calls Source: https://www.jsonrpc.org/specification/index Explains how to send multiple JSON-RPC requests in a single batch and how the server should respond. ```APIDOC ## 6 Batch Calls ### Description This section describes how to send multiple JSON-RPC requests simultaneously using a batch request and how the server should respond with a batch of responses. ### Batch Request A batch request is an Array containing multiple JSON-RPC Request objects. ```json [ { "jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1 }, { "jsonrpc": "2.0", "method": "update", "params": {"id": 2, "value": "hello"}, "id": 2 }, { "jsonrpc": "2.0", "method": "notify_sum", "params": [1, 2] // No id, this is a notification } ] ``` ### Batch Response The server MUST respond with an Array containing the corresponding Response objects for each Request object in the batch, except for notifications. The order of responses in the array MAY differ from the order of requests. ```json [ { "jsonrpc": "2.0", "result": 19, "id": 1 }, { "jsonrpc": "2.0", "result": null, "id": 2 } ] ``` ### Batch Call Error Handling * If the batch request itself is invalid (e.g., not a JSON array, or an empty array), the server MUST respond with a single Response object indicating the error. * If a batch call fails to be recognized as a valid JSON or as an Array with at least one value, the response from the Server MUST be a single Response object. * If there are no Response objects to be returned in the batch (e.g., all requests were notifications), the server MUST NOT return an empty Array and should return nothing at all. ``` -------------------------------- ### JSON-RPC Call for Non-existent Method Source: https://www.jsonrpc.org/specification/index Demonstrates the error response when a JSON-RPC call attempts to invoke a method that is not implemented on the server. It includes the client's request and the server's 'Method not found' error response. ```json --> {"jsonrpc": "2.0", "method": "foobar", "id": "1"} <-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"} ``` -------------------------------- ### JSON-RPC Call with Positional Parameters Source: https://www.jsonrpc.org/specification/index Demonstrates a JSON-RPC call using positional parameters for the 'subtract' method. It shows both the request sent to the server and the response received from the client, including successful results and different parameter orders. ```json --> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1} <-- {"jsonrpc": "2.0", "result": 19, "id": 1} --> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2} <-- {"jsonrpc": "2.0", "result": -19, "id": 2} ``` -------------------------------- ### JSON-RPC Batch Call (All Notifications) Source: https://www.jsonrpc.org/specification/index Illustrates a JSON-RPC batch request where all included requests are notifications. As per the specification, no response is returned by the server for batches containing only notifications. ```json --> [ {"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]} ] <-- //Nothing is returned for all notification batches ``` -------------------------------- ### JSON-RPC Call with Invalid Batch Source: https://www.jsonrpc.org/specification/index Demonstrates how the server handles a JSON-RPC batch request where all individual requests are invalid. Each invalid request in the batch results in a corresponding error response. ```json --> [1,2,3] <-- [ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} ] ``` -------------------------------- ### JSON-RPC 2.0 Request and Response Structure Source: https://www.jsonrpc.org/specification/index Defines the structure for JSON-RPC 2.0 requests and responses, including version, method, parameters, result, error, and ID. ```APIDOC ## JSON-RPC 2.0 Protocol Overview ### Description This section outlines the core components of a JSON-RPC 2.0 call, including the request object sent by the client and the response object returned by the server. ### Request Object Structure ```json { "jsonrpc": "2.0", "method": "someMethod", "params": [1, 2] or {"param1": "value1", "param2": "value2"}, "id": "someId" or 123 or null } ``` ### Response Object Structure ```json { "jsonrpc": "2.0", "result": "..." or null, // REQUIRED on success, MUST NOT exist on error "error": { "code": -32603, "message": "Internal error.", "data": "Optional data." }, // REQUIRED on error, MUST NOT exist on success "id": "someId" or 123 or null // MUST match the request id, or null on parse error } ``` ### Key Fields * **jsonrpc**: String - Specifies the JSON-RPC protocol version. MUST be exactly "2.0". * **method**: String - The name of the method to be invoked. * **params**: Array or Object - Contains the parameters for the method call. Can be by-position (Array) or by-name (Object). * **id**: String, Number, or Null - An identifier established by the client that MUST be included in the Response object. If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it MUST be Null. * **result**: Any - The result of the method call. REQUIRED on success. * **error**: Object - An object containing error information. REQUIRED on error. ### Error Object Structure (within Response) ```json { "code": -32700, // Integer error code "message": "Parse error", // Short description of the error "data": "Optional additional information" } ``` ### Pre-defined Error Codes * -32700: Parse error * -32600: Invalid Request * -32601: Method not found * -32602: Invalid params * -32603: Internal error * -32000 to -32099: Server error (reserved for implementation-defined errors) ``` -------------------------------- ### JSON-RPC 2.0 Batch Request Source: https://www.jsonrpc.org/specification/index Allows multiple JSON-RPC requests or notifications to be sent in a single transmission. ```APIDOC ## POST /jsonrpc (Batch) ### Description Sends multiple JSON-RPC requests and/or notifications as a single JSON Array. ### Method POST ### Endpoint `/jsonrpc` (or any transport-agnostic endpoint) ### Parameters #### Request Body - **Array of Request Objects** - REQUIRED - Each element in the array is a valid JSON-RPC 2.0 Request object or Notification. ### Request Example ```json [ { "jsonrpc": "2.0", "method": "sum", "params": [1, 2, 4, 6], "id": "req-1" }, { "jsonrpc": "2.0", "method": "notify_hello", "params": ["Hello", "World"] }, { "jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "req-2" }, { "jsonrpc": "2.0", "method": "foo.bar", "params": { "baz": "qux" }, "id": "req-3" }, { "jsonrpc": "2.0", "method": "get_data", "id": 92 } ] ``` ### Response If the batch request contains only notifications, the server MUST NOT return a Response object. If the batch request contains at least one Request object, the Server MUST return a Response object for each Request object in the batch. The Response objects MUST be in the same order as the requests in the batch array. The Server MAY return a single Response object that is an Array of Response objects. Error responses for invalid requests within the batch should be included in the response array. ### Response Example (Mixed Batch) ```json [ { "jsonrpc": "2.0", "result": 13, "id": "req-1" }, { "jsonrpc": "2.0", "error": { "code": -32601, "message": "Method not found", "data": "get_data" }, "id": 92 }, { "jsonrpc": "2.0", "result": 19, "id": "req-2" } ] ``` ``` -------------------------------- ### JSON-RPC Call with Empty Array Source: https://www.jsonrpc.org/specification/index Illustrates the error response when an empty array is sent as a JSON-RPC batch request. According to the specification, an empty array is considered an invalid request. ```json --> [] <-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} ``` -------------------------------- ### JSON-RPC Call with Invalid Batch (Non-empty) Source: https://www.jsonrpc.org/specification/index Shows the error handling for a JSON-RPC batch request that contains invalid request objects but is not empty. The server returns an 'Invalid Request' error for each invalid item in the batch. ```json --> [1] <-- [ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} ] ``` -------------------------------- ### JSON-RPC 2.0 Request Object Source: https://www.jsonrpc.org/specification/index Defines the structure of a JSON-RPC 2.0 request, used by clients to invoke methods on a server. ```APIDOC ## POST /jsonrpc ### Description Represents a remote procedure call initiated by a client to a server. ### Method POST ### Endpoint `/jsonrpc` (or any transport-agnostic endpoint) ### Parameters #### Request Body - **jsonrpc** (String) - REQUIRED - Specifies the JSON-RPC protocol version. MUST be exactly "2.0". - **method** (String) - REQUIRED - The name of the method to be invoked. Method names starting with 'rpc.' are reserved. - **params** (Structured) - OPTIONAL - A structured value holding parameter values for the method invocation. - **id** (String | Number | Null) - OPTIONAL - An identifier established by the client to correlate requests and responses. Omitted for notifications. ### Request Example ```json { "jsonrpc": "2.0", "method": "subtract", "params": [2, 5], "id": 3 } ``` ### Response #### Success Response (200) - **jsonrpc** (String) - The JSON-RPC protocol version. MUST be exactly "2.0". - **result** (any) - The result of the method invocation. - **id** (String | Number | Null) - The identifier established by the client in the Request object. #### Error Response (e.g., 400, 500) - **jsonrpc** (String) - The JSON-RPC protocol version. MUST be exactly "2.0". - **error** (Object) - An object containing error information. - **code** (Number) - A Number that indicates the error type. - **message** (String) - A String that provides a short description of the error. - **data** (any) - OPTIONAL - A primitive or structured value that contains additional information about the error. - **id** (String | Number | Null) - The identifier established by the client in the Request object. ### Response Example (Success) ```json { "jsonrpc": "2.0", "result": -3, "id": 3 } ``` ### Response Example (Error) ```json { "jsonrpc": "2.0", "error": { "code": -32601, "message": "Method not found" }, "id": "req-1" } ``` ``` -------------------------------- ### JSON-RPC Batch Call with Invalid JSON Source: https://www.jsonrpc.org/specification/index Demonstrates an error scenario where a batch of JSON-RPC requests contains malformed JSON. The server responds with a 'Parse error' for the entire batch. ```json --> [ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method" ] <-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null} ``` -------------------------------- ### JSON-RPC Call with Invalid JSON Source: https://www.jsonrpc.org/specification/index Illustrates the error response when the client sends a request with malformed JSON. The server responds with a 'Parse error', indicating that the request could not be interpreted. ```json --> {"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz] <-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null} ``` -------------------------------- ### JSON-RPC Call with Invalid Request Object Source: https://www.jsonrpc.org/specification/index Shows the error response for a JSON-RPC request that is syntactically correct JSON but violates the JSON-RPC specification, such as having a non-string method name. The server returns an 'Invalid Request' error. ```json --> {"jsonrpc": "2.0", "method": 1, "params": "bar"} <-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} ``` -------------------------------- ### JSON-RPC 2.0 Notification Source: https://www.jsonrpc.org/specification/index A special type of request that does not expect a response from the server. ```APIDOC ## POST /jsonrpc (Notification) ### Description A JSON-RPC request object without an 'id' member, signifying the client's lack of interest in a response. ### Method POST ### Endpoint `/jsonrpc` (or any transport-agnostic endpoint) ### Parameters #### Request Body - **jsonrpc** (String) - REQUIRED - Specifies the JSON-RPC protocol version. MUST be exactly "2.0". - **method** (String) - REQUIRED - The name of the method to be invoked. - **params** (Structured) - OPTIONAL - Parameter values for the method invocation. ### Request Example ```json { "jsonrpc": "2.0", "method": "update", "params": [1, 2, 3] } ``` ### Response No response is expected or should be sent by the server for a notification. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.