Enable Dark Mode!
key-elements-of-controller-request-and-response-in-odoo-16.jpg
By: Mily Shajan

Key Elements of Controller Request & Response in Odoo 16

Technical Odoo 16

In Odoo, controllers are used to handle HTTP requests and provide appropriate responses. They are responsible for defining the behavior and processing logic of different routes or endpoints in an Odoo module.
Controllers in Odoo are responsible for handling various HTTP methods such as GET, POST, PUT, DELETE, etc. They receive requests from clients, perform necessary operations, and generate appropriate responses. This allows developers to create custom web pages, APIs, and web services within an Odoo module.
In this blog post, we will explore the key elements of Controller Request and Response in Odoo16.

Request

Requests provides a convenient and intuitive API for interacting with web services, APIs, and websites.Which helps to sending HTTP requests,handling response data,customizing requests and handling errors and exceptions.
At the beginning of a request in Odoo, the request object is automatically assigned to the variable odoo.http.request.
There are mainly two types of requests :- 
HTTP Request and JSON Request
Communication between the client and server is advantageous, utilizing HTTP requests via the GET and POST methods. The client can send a request to the server either by encoding it in the URL using the GET method or by placing it in the HTTP body using the POST method.
JsonRequest facilitates client-server communication by executing methods on the server using provided arguments. The client can obtain responses from these method calls, thereby receiving relevant data.

class WebRequest(httprequest)

In Odoo, the class WebRequest is not a built-in class. However, Odoo does provide a class called HttpRequest that is commonly used for handling web requests.
The HttpRequest class is defined in the odoo.http module. It is the base class for handling HTTP requests in Odoo. This class provides various attributes and methods to interact with the incoming HTTP request.
Parameters: httprequest - a wrapped werkzeug Request object
The httprequest parameter is available in Odoo when defining a controller method that handles HTTP requests. It represents the incoming HTTP request made by a client.
Properties:
cr
It is the  database cursor object. It is a database interface provided by Odoo that allows you to interact with the database tables and perform database operations like querying, creating, updating, and deleting records.
The "cr" object is typically used within Odoo models and methods to execute database queries, perform transactions, and manage data.
context
The context can be a dictionary or an object that contains key-value pairs representing various contextual information. It can include information such as the user's session details, localization settings, authorization data, request-specific parameters, or any other relevant information that can be useful in processing the request,which helps to map the context values to the current request.
Env
It represents a database session and provides access to the models, records, and other features of the Odoo framework. The environment is a key component that allows you to interact with the database and perform various operations such as creating, reading, updating, and deleting records.
Session
each HTTP request is typically handled within a session context. The session context manages database transactions, user authentication, and other session-specific information.
DB
db parameter in an HTTP request is used to specify the target database for the request. It allows you to specify the database to which the request should be directed, enabling you to interact with a specific Odoo database when running multiple instances.
When making an HTTP request to an Odoo server, the db parameter can be included in the request URL or as a parameter in the request payload.
csrf_token(time_limit=None) - This method is used to generate a Cross-Site Request Forgery (CSRF) token. The CSRF token is a security measure to protect against CSRF attacks by ensuring that requests made to the server originate from trusted and authenticated sources.
Class HttpRequest(*args) 
The handler methods for HTTP requests in controller classes can accept keyword arguments that correspond to various types of parameters, including routing parameters, query string parameters, form parameters, and files.
Routing Parameters:- Routing parameters are extracted from the URL path and passed as keyword arguments to the handler method. These parameters are defined in the route decorator.
Query String Parameters:- Query string parameters are the key-value pairs appended to the URL.These parameters are automatically parsed by Odoo and can be accessed using the params attribute of the http.request object.
Form Parameters:- Form parameters are the data submitted through an HTML form.They can be accessed using the form attribute of the http.request object.
make_response(data, headers=None, cookies=None) - The make_response()  in Odoo is used to create an HTTP response object with the provided data, headers, and cookies. It is typically used within controller methods to construct and return the desired response.
Parameters in make_response()
Data - (basestring) – response body
Headers - Headers are key-value pairs included in the response sent by a server to a client, typically a web browser.
Cookies - Cookies are small pieces of data that the server sends to the client's browser, which are then stored and sent back with subsequent requests to the same server.
Template - template in an HTTP response by rendering the template and setting it as the response content. Odoo uses the QWeb templating engine for generating dynamic web content
qcontext - The qcontext is a dictionary that contains the context variables used during template rendering. You can manipulate the qcontext to include or modify data before rendering the template.
lazy - If it is true, the rendering of the template should be deferred until the last moment(Bool format)
Kw - kw argument in an HTTP response refers to a dictionary that holds key-value pairs representing additional arguments passed to the controller method. These additional arguments can be used to customize the behavior of the controller and the resulting HTTP response.
Class JsonRequest(*args)
Odoo  can handle JSON requests in a controller by accessing the request payload and parsing it as JSON data.
Successful request:
request:
 {"jsonrpc": "2.0",
     "method": "call",
     "params": {"context": {},
                "arg1": "val1" },
     "id": null}
response:
 {"jsonrpc": "2.0",
     "result": { "res1": "val1" },
     "id": null}
Request producing a 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)
The Response class allows you to set various attributes of the HTTP response, such as headers, content, status code, and more.it have more control over the HTTP response within your controller. You can set the content, status code, headers, cookies, and other attributes based on your requirements
Parameters:
Parameters in a response by returning a dictionary containing the desired key-value pairs
Template : which is the rendering template in the response output
qcontext : which is a dict used for rendering 
Uid : which is the current user id ,In default the value is none
This helps to get key elements and aspects of Controller Request and responses in Odoo 16 


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message