Elements of Controller Requests and Responses in Odoo 17

TEAM-BASSAM
August 5, 2024
controllers-in-odoo

The controllers in Odoo are responsible for handling HTTP requests and responding appropriately.

An Odoo module’s routing rules define the functionality and processing logic for various routes or endpoints.

Controllers in Odoo are responsible for handling various HTTP methods, including GET, POST, PUT, DELETE, and others. They take requests from clients, carry out necessary tasks, and generate pertinent answers. With this feature, developers can create unique web pages, web services, and APIs inside of an Odoo module.

Request

Using Requests, you can interact with websites, APIs, and web services in an intuitive and user-friendly way. Sending HTTP requests, managing response data, customizing requests, and handling errors and exceptions are among its functions.

Odoo automatically assigns the request object to the odoo. http.request variable at the beginning of a request.

Requests can be divided into HTTP Requests and JSON Requests.

Communication between the client and server is facilitated by using the GET and POST methods of HTTP requests. To provide flexibility and versatility in data transmission, the client can choose to send a request to the server by encoding it in the URL using the GET method or by placing it in the HTTP body using the POST method.

JsonRequest uses arguments to invoke server methods, which simplifies client-server communication. These method calls can then return responses to the client, which makes it possible to retrieve relevant data.

1. Class WebRequest(HTTP request)

There is no predefined class named WebRequest in Odoo. Odoo does, however, offer a class called HttpRequest, which is commonly used to handle web requests.

The HttpRequest class, which is the foundation for handling HTTP requests in Odoo, is hosted by the odoo.HTTP module. This class gives developers access to a wide range of properties and functions that are essential for interacting with incoming HTTP requests.

Parameters: HTTP request – an encapsulated Werkzeug Request object

When creating a controller method in Odoo that is responsible for managing HTTP requests, you can access the HTTP request parameter. It represents an incoming HTTP request that a client has made.

Properties:cr

The database cursor object in Odoo is represented by the “cr” object. It functions as an Odoo-provided database interface, allowing users to interact with database tables and carry out database operations like searching, adding, editing, and removing records.

Odoo models and methods commonly use the “cr” object to execute database queries, conduct transactions, and oversee data management.

Context

The context, which can be an object or a dictionary, consists of key-value pairs that represent various contextual details. This includes information about the user’s session, localization preferences, authorization data, request-specific parameters, and any other relevant details that are essential to the request’s processing. Mapping these values to the current request is made easier by utilizing the context, which promotes smooth processing.

Env

The Odoo environment represents a database session that provides access to records, models, and other features within the Odoo framework. It is an essential component that allows users to engage with the database and perform various operations such as creating, retrieving, updating, and deleting records.

Session

It is customary to operate within a session context for every HTTP request. Database transactions, user authentication, and other session-specific information are managed in this context.

DB

The target database for a given HTTP request is identified by the ‘db’ parameter. This parameter allows you to specify the exact database that the request should communicate with. This is especially helpful in situations where there are several Odoo database instances.

The ‘db’ parameter can be included either directly in the request URL or as a parameter in the request payload when making an HTTP request to an Odoo server.

* csrf_token(time_limit=None)

To create a Cross-Site Request Forgery (CSRF) token, use this technique. As a preventative measure against cross-site request forgeries (CSRF) attacks, the CSRF token ensures that requests made to the server come only from verified and trusted sources.

* Class HttpRequest(*args) 

Controller classes have handler methods for HTTP requests that can take keyword arguments corresponding to different kinds of parameters. These parameters allow for flexible handling of incoming requests and can include routing parameters, query string parameters, form parameters, and files.

* Routing Parameters

The handler method receives the extracted parameters from the URL path as keyword arguments. The route decorator defines these parameters explicitly.

* Query String Parameters

Key-value pairs that are appended to the end of the URL make up the query string parameters. These parameters are automatically parsed by Odoo and are accessible via HTTP. request object’s params attribute.

* Form Parameters

The data entered into an HTML form is referred to as form parameters. The HTTP. request object’s form attribute can be used to access these parameters.

* make_response(data, headers=None, cookies=None)

To create an HTTP response object with the supplied data, headers, and cookies, use Odoo’s make_response() function. It is frequently used in controller methods to formulate and send the intended answer. Among the parameters used by make_response() are:

1. Dta: The information that will be in the body of the response.

2. Headers: Not required. Extra HTTP headers should be sent with the answer.

3. Cookies: Not required. Cookies that will be stored in the reply.

4. Template: Odoo renders a template and designates it as the response content when it is included in an HTTP response. To create dynamic web content, Odoo uses the QWeb templating engine.

5. context: Dictionary containing context variables used in the rendering of templates. It lets you edit or add information before the template.

6. Lazy: This Boolean-formatted setting, when set to True, delays the rendering of the template until the very last minute.

7. A dictionary with key-value pairs is called Kw. These pairs stand for extra arguments that are supplied to the controller method, enabling customization of the behaviour of the controller and the HTTP response that is returned.

2. Class JsonRequest(*args)

Request fulfilled successfully:

By gaining access to and parsing the request payload as JSON data, Odoo can manage JSON requests inside a controller.

Request:

{"jsonrpc": "2.0",

      "method": "call",

     "params": {"context": {},

       "arg1": "val1"},

       "id": null} 

Response:

{"jsonrpc": "2.0",

       "result": {"res1": "val1"},

       "id": null}      

Request producing an error:

Request:

{"jsonrpc": "2.0",

   "method": "call",

   "params": {"context": {},

              "arg1": "val1" },

   "id": null}

Response:

{"jsonrpc": "2.0",

    "error": {"code": 1,

              "message": "End user error message.",

              "data": {"code": "codestring",

                       "debug": "traceback" } },

    "id": null}

Response

class Response(*args, **kw)

You can alter the headers, content, status code, and other elements of an HTTP response by using the Response class. It gives you more control over the HTTP response inside your controller by letting you customize headers, cookies, content, status codes, and other elements as needed.

Parameters:

Provide a dictionary containing the required key-value pairs so that the response’s parameters can be set.

Template: The template used to render the response output.

context: A dictionary used for rendering.

Uid: The current user ID, with a default value of None.

This facilitates obtaining key elements and aspects of controller requests and responses in Odoo 17.

"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 *