Macaulay2 » Documentation
Packages » JSONRPC » JSONRPCServer » handleRequest
next | previous | forward | backward | up | index | toc

# handleRequest -- handle a JSON-RPC request

- Usage:
    `handleRequest(server, str)`
- Inputs:
  - server, a JSON-RPC server,
  - str, a string, a JSON-RPC 2.0 request object;
- Outputs:
  - a string or null, the response to the request or null if the request was a notification

## Description

This method processes an incoming JSON-RPC request. It takes a JSON-encoded string representing a request (or batch of requests) and returns the corresponding JSON-RPC response. It handles method invocation, error handling, and batching automatically. Note that makeRequest is useful for constructing requests.

    i1 : server = new JSONRPCServer

    o1 = server

    o1 : JSONRPCServer

    i2 : registerMethod(server, "sum", plus)

    i3 : handleRequest(server, makeRequest("sum", {2, 3}, 1))

    o3 = {"result": 5, "jsonrpc": "2.0", "id": 1}

    i4 : handleRequest(server, makeRequest({("sum", {4, 5}, 2), ("sum", {6, 7}, 3)}))

    o4 = [{"result": 9, "jsonrpc": "2.0", "id": 2}, {"result": 13, "jsonrpc":
         "2.0", "id": 3}]

If you need to do any error handling in a method, then the JSONRPCError class may be useful.

### Menu

- JSONRPCError -- class for JSON-RPC errors

## Ways to use handleRequest:

- `handleRequest(JSONRPCServer,String)`

## For the programmer

The object handleRequest is a method function.

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/JSONRPC.m2:363:0.