Enable Dark Mode!
json-rpc-api-odoo-13.png
By: Muhammed Nishad

How to use JSON RPC API in Odoo 13

Odoo is well known for its robust features and rich set of APIs offering complete flexibility and security in its APIs just like its user interface. Additionally, all the security measures taken in Odoo are maintained in it’s API’s. Odoo APIs work with its ORM (Object Relational Mapping) which the API users can access with their access rights.

This blog will provide an insight on How to use JSON RPC API in Odoo 13?

Odoo provides two types of prebuilt APIs: the XMLRPC API and JSONRPC API. The JSONRPC API can be used in different programming languages like Java, JavaScript, PHP, etc. Odoo JSONRPC API can be used in python3 which is explained here with an example. A file odoo_api_login.py is created and then a simple code is being used to accept username, password, host URL, host port, and database name. Furthermore, a login request is created for Odoo and prints the response.


Let’s create a python file using nano edit, which doe with the help of the following command:


cybrosys@cybrosys:~$ nano odoo_api_login.py
The results will be the creation of an empty Python file if not already existing, and the following code is added to the file:
import json import random import urllib.request Import getpass host = input(“Please enter the host URL”) port = input(“Please provide the host PORT”) database = input(“Please provide the database name which you want to access”) user = input(“Please enter the username”) password = getpass.getpass(prompt=“Please enter the password”) def json_rpc(url, method, params): data = { "jsonrpc": "2.0", "method": method, "params": params, "id": random.randint(0, 1000000000), } req = urllib.request.Request(url=url, data=json.dumps(data).encode(), headers={ "Content-Type":"application/json", }) reply = json.loads(urllib.request.urlopen(req).read().decode('UTF-8')) if reply.get("error"): raise Exception(reply["error"]) return reply["result"] def call(url, service, method, *args): return json_rpc(url, "call", {"service": service, "method": method, "args": args}) url = "http://%s:%s/jsonrpc" % (host, port) uid = call(url, "common", "login", database, user, password) print(uid)
Let me describe the above code, in the first 4 lines of the code, we imported the necessary packages for our program which are the ones described below:
- json - this package is used to handle JSON files (like converting dict to json, json to dict).
- random - a package that helps to generate random numbers between a range.
- urllib.request - the request class form urllib in order to generate the request.
- getpass - order to hide the password.
Once the necessary packages are being imported now we will accept all the five parameters that we need to be connected to the Odoo platform:
1. Host URL
2. Host Port
3. Database Name
4. Username
5. Password
Further, we created a function named json_rpc that will accept URL, request method and parameters. Moreover, make a request and return the response if success,  else raise an exception.
In addition, we will create another function named call which accepts the URL, service type, request method, and arguments and will call the json_rpc function. Finally, we printed what the function returned which is an integer user ID on success because we have used the login service here. This is how the jSON RPC API functions in Odoo 13.


Odoo Training Package
Odoo Implementation Package

If you need any assistance in odoo, we are online, please chat with us.



1
Comments

pankaj

how to use this code

19/02/2021

-

4:19AM



Leave a comment

 
whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message