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
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)