How it works...

The JSON-RPC is using the JSON format to communicate with the server. It uses the /jsonrpc endpoint to communicate with the server. In our example, we used the Python requests package to make POST requests, but if you want to you can use other packages, such as urllib.

The JSON-RPC only accepts a payload formatted in the JSON-RPC 2.0 specification, refer to; this link to learn more about the JSON-RPC format https://www.jsonrpc.org/specification. In our example, we created the get_json_payload() method. This method will prepare the payload in the valid JSON-RPC 2.0 format. This method accepts the service name and the method to call and the remaining arguments will be placed in *args. We will be using this method in all subsequent recipes. The JSON-RPC  accepts requests in JSON format and these requests are only accepted if the request contains a {"Content-Type": "application/json"} header. The result of the requests will be in the JSON format.

Like XML-RPC, all public methods, including login, come under the common service. For this reason, we passed common as a service and login as a method to prepare the JSON payload. The login method required some extra arguments, so we passed database name, username, and password. Then we made the POST request to the JSON endpoint with the payload and headers. If you passed the correct username and password, the method returns the user id. The response will be in the JSON format and you will get the result in the result key.

Note that the get_json_payload() method created in this recipe is used to remove repetitive code from the example. It is not compulsory to use it, so feel free to apply your own adaptations. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset