Enable Dark Mode!
how-to-call-json-rpc-to-webcontroller-in-odoo-16.jpg
By: Aswathi pn

How to Call JSON RPC to Webcontroller in Odoo 16

Technical Odoo 16

JSON-RPC

JSON-RPC (Remote Procedure Call) is a protocol that allows you to perform remote procedure calls over HTTP using JSON (JavaScript Object Notation) as the data format. JSON-RPC is the preferred method of communication between client software and the Odoo server in version 16.

With JSON-RPC, you can send requests to the Odoo server and receive responses in a structured JSON format. This allows you to interact with Odoo's backend functionality programmatically, execute methods, fetch data, and perform various operations.

The basic structure of a JSON-RPC request consists of the following elements:

jsonrpc: Specifies the version of the JSON-RPC protocol being used (e.g., "2.0").

method: Specifies the name of the method to be executed on the server.

params: Contains the parameters to be passed to the method.

id: An identifier for the request. The server includes this identifier in the response, allowing the client to associate the response with the corresponding request.

Web controller

In Odoo 16, a web controller is a mechanism that allows you to handle HTTP requests and provide responses within the Odoo web framework. It enables you to define custom routes and implement the corresponding logic to handle those requests.

A web controller in Odoo is implemented as a Python class that extends the http.Controller class from the odoo.http module. Within this class, you define methods that correspond to different routes, and each method handles the logic for that particular route.

Here's an example of a simple web controller in Odoo 16:   

from odoo import http
from odoo.http import request
class MyWebController(http.Controller):
@http.route('/my_module/my_controller', auth='user')
    def my_controller_method(self, **kwargs):
        # Your controller logic goes here
        # You can access the request parameters using the **kwargs dictionary
        return "Hello, World!"
In this example, we define a web controller class called MyWebController. It has a single method, my_controller_method, which handles the route /my_module/my_controller. Only users who have been authenticated can access this route, according to the auth='user' parameter.
JSON Request
First, create a new web controller in your custom module. Create a Python file, let's say controllers.py, and define your controller inside it. Here's an example:
from odoo import http
from odoo.http import request
class MyWebController(http.Controller):
    @http.route('/my_module/my_controller', type='json', auth='user')
    def my_controller_method(self, **kwargs):
        # Your controller logic goes here
        # You can access the request parameters using the **kwargs dictionary
        return {'result': 'success'}
The type in the web controller needs to be JSON in order for the controller to receive data from JSON.
Create procedures to receive JSON requests by including JSON in the type argument for the http.route() function. The JSON-RPC protocol is used by Odoo to communicate with these methods. Methods that return JSON must do so. Unless they are JSON-RPC parameters, they receive arguments as named parameters.
To provide JSON data to Python code, we need a.js file. For that, create a file test.js with the following lines:
var ajax = require('web.ajax');//To define ajaxvar
 test_variable = document.getElementById("test_variable_id").value;//To get value from the required field.
ajax.jsonRpc('/my_module/my_controller, 'call', {                       
'test_variable' : test_variable,                      
});//json Rpc Call
.then(function (data) {});
//To receive data from python(non-mandatory)
Here, we call ajax.jsonRpc and give the value test_value to the corresponding URL of the controller. One can choose to run directly from the controller after executing operations in the method (from the controller), or one can return the result to the js file.
This way, we can call the web controller from the JS file using JSON RPC. 


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

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