Offering a wide range of features, Odoo stands out as a complete business management software suite in the web development industry. The capability to develop unique web controllers that manage requests and provide responses is one of its primary characteristics. Communicating with these controllers in Odoo 18 via JSON RPC (Remote Procedure Call) is a powerful way to increase the platform's functionality.
JSON RPC allows developers to invoke processes on remote servers using JSON as the data transport protocol. This makes it possible for various Odoo application components to communicate with one another seamlessly. This tutorial will show you how to use Odoo 18's web controllers to use JSON RPC, opening up the possibility of creating dynamic and interactive web applications.
Understanding JSON RPC
JSON Remote Procedure Call, or JSON-RPC for short, is a protocol that allows remote procedure calls through HTTP using JSON (JavaScript Object Notation) as the selected data format. In Odoo version 18, JSON-RPC is the suggested protocol for client software to communicate with the Odoo server.
JSON-RPC makes it simpler to send queries to the Odoo server and receive structured JSON responses in return. Programmatic connection with Odoo's backend features is made possible by this structured format, which permits method execution, data retrieval, and other operational duties.
Web controller
In Odoo, a web controller is a Python class used to handle HTTP requests and return HTTP responses, usually as HTML, JSON, or other formats. It is part of Odoo's web framework and is based on the Werkzeug library. The primary purpose of this technique is to make certain routes (URLs) accessible through a browser or HTTP clients (such as Postman or frontend JS code). It’s particularly useful when creating custom frontend pages, portals, APIs, or integrations.
Below is a basic web controller in Odoo 18:
from odoo import http
class TestController(http.Controller):
@http.route('/my/route',type='json', auth='user', website=True)
def test_method(self, **kwargs):
# Your code logic here
return "Webcontroller in Odoo18"
In this example, we create a web controller named `TestRpcController`. It contains one method called `test_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
Use the `http.route()` function and pass JSON as the type argument to create procedures that can process JSON requests. To communicate with these methods, Odoo uses the JSON-RPC protocol, which requires that methods that return JSON data follow this protocol. Unless they are specific to JSON-RPC parameters, parameters for JSON-RPC methods are received as named parameters.
/** @odoo-module **/
import publicWidget from "@web/legacy/js/public/public_widget";
import { rpc } from "@web/core/network/rpc";
import { onWillStart } from "@odoo/owl";
publicWidget.registry.TestRpcController = publicWidget.Widget.extend({
selector: ".o_test_widget",
start: function () {
return this._super(...arguments).then(async () => {
const data = await rpc("/my/route", {});
});},
});
Both `PublicWidget` and `rpc` are imported in this example of code. By extending `PublicWidget`, it then generates a new widget called `TestRpcController`. It starts a JSON-RPC call to the `/my/route` endpoint in the `onWillStart` method of `TestRpcController` and waits for a response. Following the method's execution within the controller, the user can either return the result to the JavaScript file or immediately initiate operations. This technique makes using JSON-RPC to call the web controller from the JS file easier.
To sum up, Odoo 18's JSON-RPC to web controllers feature offers a world of customization and extension options for your Odoo apps. By using JSON-RPC, you can quickly add unique functionality to your web apps and make them more dynamic and interactive.
To read more about How to call JSON RPC to Webcontroller in Odoo 19, refer to our blog How to call JSON RPC to Webcontroller in Odoo 19.