How to Call JSON RPC to Web controller in Odoo17

ANURAG
July 9, 2024
json-rpc-to-web-controller

Odoo ERP can described in the world of web development as a business management software that has numerous features. When it comes to the capabilities of the framework, one can mention the ability to implement custom web controllers that process requests and return responses. When working with Odoo17, it is possible to communicate with these controllers based on the JSON RPC (Remote Procedure Call) and it is quite effective in terms of the expansion of the functionality of the platform.  in this blog, we are going to explore how to Call JSON RPC to Web controller in Odoo17 to enable the creation of Web 2.0 Applications


JSON RPC is a protocol that enables a developer to call procedures on another server and use JSON as the format for data interchange. This helps in integrating various parts of an Odoo application so that they can easily communicate with each other.

Understanding JSON RPC

JSON-RPC stands for JavaScript Object Notation Remote Procedure Call and is a protocol for performing remote procedure calls over HTTP with JSON being the data interchange format. In the 17th version of Odoo ERP, JSON-RPC is the preferred method of communication between the client application and the Odoo ERP server.
JSON-RPC, designed to simplify the sending of requests to the Odoo server and get back a structured JSON response. Due to this structured format, it is possible to program and interact with most of the Odoo backend functionalities including method calls, data pulls, and operational activities.

undefined
undefined
– jsonrpc: This field indicates the version of the JSON-RPC protocol.
– method: It specifies the name of the method to executed on the server.
– params: This component includes the arguments to passed to the said method in order to help deliver essential information for processing.
– id: As an identifier for the request, this element enables the server to place it in the response and make it easy for the client to match requests with responses.

Web controller

It acts as a means of handling HTTP requests and returning responses within the Odoo ERP web architecture. It allows developers to set up custom paths and perform the related operations.



When implemented, the web controller as a Python class derived from HTTP. Controller class from the odoo. undefined In this class, developers declare methods where each of them represents a specific route and performs the logic assigned to that route.undefined

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 new web controller called MyController. It has only one method, my_method which deals with the request that to the ‘/my/route’ route. This route can only by users that have logged in to the application.

Calling JSON RPC to Web Controllers

When your web controller is ready you can call its methods using JSON RPC. undefined

To set up procedures that can handle JSON requests, use the http. route() function and pass JSON as the type argument. Odoo uses JSON-RPC to call these methods, which means that methods that return JSON data must conform to this protocol. Parameters for JSON-RPC methods are passed as name/value pairs unless they are related to JSON-RPC parameters.

For Python code to be able to parse JSON data, there is the need to create a . undefined For this purpose, create a file named test. undefined

/** @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', {})
       },
});

In this code, the definition of the module “Weblicts” is witnessed to be importing both `PublicWidget and jsonrpc`. The above code further proceeds to declare a new widget called `TestController` which is a subclass of the `PublicWidget`. Contained in the `will start` method of ‘TestController’, it then performs the JSON-RPC request to the tackled point ‘/my/route’(The reaction is expected as a JSON-RPC callback from this route next). To get the method implemented in the controller, the person can either call the operations right away or pass it down to the JavaScript file. It allows for making a call to the web controller of the application from the JS file through JSON RPC To Web Controller
To summarize, one can conclude that using JSON RPC to webcontroller in Odoo17 is effective in terms of expanding and developing applications in Odoo to the level that a certain developer may want. Thanks to JSON RPC it is possible to enrich growth and functions, develop special procedures, and incorporate them into web applications.





"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *