Odoo 19 introduces a powerful and structured way to extend the platform through web controllers. Controllers allow developers to define custom routes that can respond to HTTP or JSON-RPC requests, making it possible to integrate dynamic features into web applications.
In this article, we’ll explore how to call JSON RPC to web controllers in Odoo 19, with step-by-step examples for both the backend and frontend.
What is JSON RPC?
JSON-RPC (Remote Procedure Call) is a protocol that enables communication between a client and a server using JSON as the data exchange format. In Odoo 19, JSON-RPC routes are defined with type='jsonrpc', making it easy to send structured requests and receive structured responses.
A typical JSON-RPC request looks like this:
{
"jsonrpc": "2.0",
"method": "call",
"params": { "name": "Odoo Developer" },
"id": null
}
And the response is:
{
"jsonrpc": "2.0",
"result": { "greeting": "Hello, Odoo Developer!" },
"id": null
}
Creating a Web Controller in Odoo 19
To define a controller, inherit from odoo.http.Controller and use the @http.route() decorator. For JSON-RPC communication, specify type='jsonrpc'.
Example: Python Controller
from odoo import http
class MyController(http.Controller):
@http.route('/my/jsonrpc', type='jsonrpc', auth='user', methods=['POST'])
def my_method(self, name):
return {"message": f"Hello, {name}! Welcome to Odoo 19."}
Key Points:
- type='jsonrpc' > Enables JSON-RPC communication.
- auth > Can be user, public, none, or bearer depending on the access you want.
- Return values > Must be JSON serializable (e.g., dict, list, string, number).
Calling JSON RPC from JavaScript
On the frontend, Odoo provides the jsonrpc utility to easily call backend JSON-RPC routes. With Odoo 19’s OWL framework, this integrates seamlessly into components.
Example: JavaScript Component
/** @odoo-module **/
import { jsonrpc } from "@web/core/network/rpc_service";
import { Component, onMounted } from "@odoo/owl";
export class TestComponent extends Component {
setup() {
onMounted(async () => {
const response = await jsonrpc("/my/jsonrpc", { name: "Odoo 19 Developer" });
console.log("Response from server:", response);
alert(response.message);
});
}
}
What happens here:
- The frontend calls /my/jsonrpc with { name: "Odoo 19 Developer" }.
- The controller receives the request, executes logic, and returns JSON.
- The response is displayed in the browser.
Example: Full Flow in Odoo 19
Backend Controller
from odoo import http
class ExampleController(http.Controller):
@http.route('/example/jsonrpc', type='jsonrpc', auth='public')
def example_json(self, name):
return {"greeting": f"Hello, {name}!"}
Frontend JavaScript
/** @odoo-module **/
import { jsonrpc } from "@web/core/network/rpc_service";
import { Component, onMounted } from "@odoo/owl";
export class ExampleComponent extends Component {
setup() {
onMounted(async () => {
const result = await jsonrpc("/example/jsonrpc", { name: "Odoo Enthusiast" });
alert(result.greeting);
});
}
}
When the component loads, the browser will display:
“Hello, Odoo Enthusiast!”
Conclusion
Calling JSON RPC to web controllers in Odoo 19 provides a clean, structured, and efficient way to connect frontend components with backend logic. By using type='jsonrpc' routes and the jsonrpc utility in JavaScript, developers can build highly interactive and dynamic applications directly within Odoo 19.
This approach is essential for creating custom APIs, enhancing website features, and ensuring smooth communication between client and server in modern Odoo applications.
To read more about How to Call JSON RPC to Webcontroller in Odoo 17, refer to our blog How to Call JSON RPC to Webcontroller in Odoo 17.