Django-modern-rpc is a Django library that helps you enable JSON and XML-RPC servers in your application. It is easy to install and configure, and highly customizable. This website shows the library in action.
Here is the full list of procedures exposed by this demo application. You can read the documentation for each one and copy/paste the command into your preferred terminal to try it by yourself. Both sync and async versions of the view are available. Use the selector on top-right to switch between them. The currently select server is available to RPC calls at this URL:
Raise a custom RPC exception.
:raises RPCException: Always raised with a custom error code and message.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>errors.custom</methodName>
<params>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 5409,
"method": "errors.custom",
"params": []
}'
Return a value that cannot be serialized by the RPC transport.
Specifically, this procedure returns an instance of Python’s built-in Fraction class, which is not JSON-serializable by default.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>errors.unserializable_result</methodName>
<params>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 3672,
"method": "errors.unserializable_result",
"params": []
}'
Fetch many URLs concurrently with a concurrency limit.
This demonstrates awaiting multiple async tasks via asyncio.Semaphore and asyncio.gather.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>http.fetch_many</methodName>
<params>
<param>
<value><string>undefined</string></value>
</param>
<param>
<value><string>undefined</string></value>
</param>
<param>
<value><int>937</int></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 7764,
"method": "http.fetch_many",
"params": [
"undefined",
423.894,
254
]
}'
Perform an async HTTP GET request and parse JSON body.
:raises httpx.HTTPError: On network or protocol errors :raises ValueError: If the response body is not valid JSON
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>http.get_json</methodName>
<params>
<param>
<value><string>abcde</string></value>
</param>
<param>
<value><string>undefined</string></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 299,
"method": "http.get_json",
"params": [
"abcde",
"undefined"
]
}'
Perform an async HTTP GET request and return response metadata and text body.
:raises httpx.HTTPError: On network or protocol errors
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>http.get_text</methodName>
<params>
<param>
<value><string>abcde</string></value>
</param>
<param>
<value><string>undefined</string></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 3460,
"method": "http.get_text",
"params": [
"abcde",
"undefined"
]
}'
Await for the given number of seconds, then return the same value.
This demonstrates a minimal async RPC procedure that awaits an IO-bound task.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>http.sleep</methodName>
<params>
<param>
<value><double>624.12</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 683,
"method": "http.sleep",
"params": [
46.212
]
}'
Returns the sum of two or more terms.
This procedure can be used to test variable number of inputs
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>math.add</methodName>
<params>
<param>
<value><double>69.384</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 7727,
"method": "math.add",
"params": [
623
]
}'
Returns the division result of two numbers.
This procedure can be used to raise an exception. If divisor is set to 0, a ZeroDivisionError will be raised from the function and transformed into a proper RPC Error response.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>math.divide</methodName>
<params>
<param>
<value><double>379.262</double></value>
</param>
<param>
<value><int>881</int></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 9217,
"method": "math.divide",
"params": [
613.25,
502
]
}'
Return the product of two or more numbers.
:raises ValueError: If fewer than two factors are provided
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>math.multiply</methodName>
<params>
<param>
<value><int>10</int></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 6054,
"method": "math.multiply",
"params": [
342
]
}'
Raise a number to a given power.
This is equivalent to Python's base ** exponent.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>math.power</methodName>
<params>
<param>
<value><double>134.16</double></value>
</param>
<param>
<value><double>73.901</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 662,
"method": "math.power",
"params": [
568,
161
]
}'
Return the non-negative square root of x.
This is a thin wrapper around math.sqrt. If x is negative, math.sqrt raises ValueError which will
be converted into a proper RPC error by the framework.
:raises ValueError: If x is negative
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>math.sqrt</methodName>
<params>
<param>
<value><int>79</int></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 5573,
"method": "math.sqrt",
"params": [
668.945
]
}'
Return the result of subtracting one or more numbers from a first value.
:raises ValueError: If no subtrahends are provided
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>math.subtract</methodName>
<params>
<param>
<value><int>499</int></value>
</param>
<param>
<value><double>843.862</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 2351,
"method": "math.subtract",
"params": [
865.352,
501.265
]
}'
Returns a list of all procedures exposed by the server
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>system.listMethods</methodName>
<params>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 9844,
"method": "system.listMethods",
"params": []
}'
Returns the documentation of the given procedure.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>system.methodHelp</methodName>
<params>
<param>
<value><string>abcde</string></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 7957,
"method": "system.methodHelp",
"params": [
"abcde"
]
}'
Returns an array describing the possible signatures for the given procedure.
Currently, this procedure only returns one possible signature, so the result is a list of 1 list.
The inner list contains:
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>system.methodSignature</methodName>
<params>
<param>
<value><string>abcde</string></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 891,
"method": "system.methodSignature",
"params": [
"abcde"
]
}'
Call multiple procedure at once.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>system.multicall</methodName>
<params>
<param>
<value><string>undefined</string></value>
</param>
</params>
</methodCall>'
Inspect the incoming request and extract the Content-Type header if present. This procedure demonstrates how a remote procedure can access the request object.
curl -X POST '' \
--header 'Content-Type: application/xml' \
--data '<?xml version="1.0"?>
<methodCall>
<methodName>utils.printContentType</methodName>
<params>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 149,
"method": "utils.printContentType",
"params": []
}'
The source code of this website is available on GitHub. If you find any issue or have a suggestion, please open an issue on the project's repository.
If you use this library in your project and want to help increasing its visibility, just go to djangopackages and vote for the library. Thank you!