In the world of web development, Odoo stands out as a comprehensive business management software suite, offering a wide range of functionalities. One of its key features is the ability to create custom web controllers that handle requests and serve responses. In Odoo 17, interacting with these controllers via JSON RPC (Remote Procedure Call) is a powerful way to extend the platform's capabilities.
JSON RPC allows developers to invoke procedures on remote servers using JSON as the data interchange format. This enables seamless communication between different components of an Odoo application. In this guide, we'll explore how to call JSON RPC to web controllers in Odoo 17, unlocking the potential for building dynamic and interactive web applications.
Understanding JSON RPC
JSON-RPC, short for JSON Remote Procedure Call, serves as a protocol facilitating remote procedure calls via HTTP, with JSON (JavaScript Object Notation) as the chosen data format. In version 17 of Odoo, JSON-RPC stands as the recommended approach for communication between client software and the Odoo server.
JSON-RPC streamlines the process of sending requests to the Odoo server and receiving structured JSON responses in return. This structured format enables programmatic interaction with Odoo's backend functionalities, allowing for method execution, data retrieval, and various operational tasks.
The fundamental components of a JSON-RPC request encompass the following elements:
JSON-RPC entails several essential elements for effective communication between client and server:
- jsonrpc: This field specifies the version of the JSON-RPC protocol.
- method: It designates the name of the method intended for execution on the server.
- params: This component contains the parameters to be transmitted to the specified method, aiding in providing necessary data for processing.
- id: Serving as a unique identifier for the request, this element allows the server to include it in the response, facilitating the association of responses with their corresponding requests on the client side.
Web controller
The web controller serves as a mechanism for managing HTTP requests and delivering responses within the Odoo web framework. It empowers developers to define personalized routes and execute the associated functionalities.
Implemented as a Python class, the web controller extends the `http.Controller` class from the `odoo.http` module. Within this class, developers define methods, each corresponding to a distinct route, responsible for executing the logic designated for that route.
Below is an illustration of a basic web controller in Odoo 17:
from odoo import http
class MyController(http.Controller):
@http.route('/my/route', auth='user', website=True)
def my_method(self, **kwargs):
# Your code logic here
return "Hello from Odoo!"
In this example, we create a web controller named `MyController`. It contains one method called `my_method`, which handles requests sent to the ‘/my/route’ route. Only users who have logged in can access this route.
Calling JSON RPC to Web Controllers
Once you have your web controller set up, you can call its methods using JSON RPC. Here's a step-by-step guide.
To set up procedures that can handle JSON requests, utilize the `http.route()` function and specify JSON as the type argument. Odoo employs the JSON-RPC protocol for communication with these methods, necessitating that methods returning JSON data adhere to this protocol. Parameters for JSON-RPC methods are received as named parameters unless they pertain to JSON-RPC parameters themselves.
To enable Python code to interact with JSON data, it's essential to create a `.js` file. For this purpose, create a file named `test.js` containing the following lines:
/** @odoo-module */
import PublicWidget from "@web/legacy/js/public/public_widget";
import { jsonrpc } from "@web/core/network/rpc_service";
var TestController = PublicWidget.Widget.extend({
willStart: async function () {
const data = await jsonrpc('/my/route', {})
},
});
This code snippet imports `PublicWidget` and `jsonrpc`. Then, it creates a new widget named `TestController`, extending `PublicWidget`. In the `willStart` method of `TestController`, it initiates a JSON-RPC call to the `/my/route` endpoint and awaits the response. After executing the method within the controller, one has the choice to directly trigger operations or return the result to the JavaScript file. This method facilitates calling the web controller from the JS file through JSON RPC.
In conclusion, calling JSON RPC to web controllers in Odoo 17 opens up a world of possibilities for extending and customizing your Odoo applications. By leveraging the power of JSON RPC, you can seamlessly integrate custom functionality into your web applications, making them more dynamic and interactive.