Enable Dark Mode!
how-to-configure-the-json-rpc-api-in-odoo-15.jpg
By: Dino Varghees

How to Configure the JSON RPC API in Odoo 15

Technical Odoo 15

Odoo is well-known for its powerful choices and intensive API set, which, like its program, provide complete flexibility and security. Additionally, all of Odoo's security precautions are retained in its APIs.The ORM (Object Relational Mapping) that Odoo APIs use is accessible to API users based on their access rights.

This blog will discuss how JSON RPC API is used in Odoo 15.

XMLRPC API and JSONRPC API are two types of out-of-the-box APIs provided by odoo.

The JSONRPC API is compatible with various computer languages, including Java, JavaScript, PHP, and others. The Odoo JSONRPC API can be utilized with Python 3, as seen in this example.

A file called odoo API login.py Then, a simple function is developed to accept password, username,  host port, host URL, and database name. Additionally, an Odoo login request is made, and the response is printed.

Let's use nano edit to create a python file, which we can do with the following command:

nano odoo_api_login.py.

If an empty Python file does not already exist, it will be created, and the following code will be put to it:

import json
import random
import urllib.request
HOST = 'localhost'
PORT = 8015
DB = input("Please specify the name of the database you wish to access:")
USER = input("Please enter the username")
PASS = input("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", DB, USER, PASS)
print(url)
print("uid",uid)

In the above code, the first four lines are used to import the required packages for our program:

a) json - This package is used to process JSON files (eg json to dict and dict to json conversion).

b) random - This is a library used to generate random numbers within a specified range.

c) urllib.request - To generate the request, use the request class from urllib.

After we've imported all of the necessary packages, we'll accept all five parameters that we'll need to connect to the Odoo platform:

1. Host URL

2. Host Port

3. Database Name

4. Username

5. Password

We also developed the json rpc function, which accepts the URL, request method, and parameters. Furthermore, make a request and, if successful, return the result; otherwise, raise an exception.

Then we will be creating a call function, which calls the json rpc function with the URL, request method, service type, and parameters. Finally, because we used the login service, we reported the function's outcome, which on success is an integer user ID. In Odoo 15, the JSON RPC API works like this.

How to we can create a new Record using Json Rpc?

import json
import random
import urllib.request
HOST = 'localhost'
PORT = 8015
DB = input("Please specify the name of the database you wish to access:")
USER = input("Please enter the username : ")
PASS = input("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})
# log in the given database
url = "http://%s:%s/jsonrpc" % (HOST, PORT)
uid = call(url, "common", "login", DB, USER, PASS)
print("uid",uid)
#create a new note
args = {
   'color': 8,
   'memo': 'Hai note created using jsonrpc',
   'create_uid': uid,
}
note_id = call(url, "object", "execute", DB, uid, PASS, 'note.note', 'create', args)
#Reading the created note
note_id = call(url, "object", "execute", DB, uid, PASS, 'note.note', 'read', [9])
print(note_id)

how-to-configure-the-json-rpc-api-in-odoo-15-cybrosys

In conclusion, the use of the JSON RPC API can be used in the Odoo platform for the configurations of the various aspects in the Odoo platform.


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



0
Comments



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