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 documentation for each one and copy/paste the command to test it by yourself.
You can try the library right now. Simply make RPC calls using your preferred client to the address:
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": 2997,
"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": 6282,
"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><double>464.994</double></value>
</param>
<param>
<value><int>655</int></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 2220,
"method": "http.fetch_many",
"params": [
"undefined",
582.045,
286
]
}'
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": 8868,
"method": "http.get_json",
"params": [
"abcde",
288.436
]
}'
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><double>998.752</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 9107,
"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>179.706</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 1845,
"method": "http.sleep",
"params": [
781.387
]
}'
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>546.915</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 4930,
"method": "math.add",
"params": [
977.887
]
}'
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>355.166</double></value>
</param>
<param>
<value><int>36</int></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 8035,
"method": "math.divide",
"params": [
226,
805.371
]
}'
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><double>337.122</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 4640,
"method": "math.multiply",
"params": [
173.93
]
}'
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><int>879</int></value>
</param>
<param>
<value><int>779</int></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 7905,
"method": "math.power",
"params": [
103,
582.393
]
}'
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>15</int></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 1713,
"method": "math.sqrt",
"params": [
126.566
]
}'
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><double>427.161</double></value>
</param>
<param>
<value><double>264.046</double></value>
</param>
</params>
</methodCall>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 5349,
"method": "math.subtract",
"params": [
295,
814
]
}'
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": 615,
"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": 510,
"method": "system.methodHelp",
"params": [
"abcde"
]
}'
Returns an array describing the signature of the given procedure.
The result is a list with:
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": 2402,
"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>'
curl -X POST '' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": 9771,
"method": "system.multicall",
"params": [
"undefined"
]
}'
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": 8007,
"method": "utils.printContentType",
"params": []
}'
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!