Enable Dark Mode!
controller-request-and-response-in-odoo-14.jpg
By: Arunima V

Controller Request and Response in Odoo 14

Technical

In Odoo, frontend modules are developed by controllers. When a user needs to render some data into a website controller that handles an HTTP request, this class can be used; let’s take a look at Odoo’s web controller’s request and response.
This blog will define the aspects of the Controller Request and Response in Odoo 14.
Request
Data are transferred from their data source through requests. In Odoo, the request object is automatically put on odoo.http.request at the start of the request. There are two types of requests - jsonrequest and httprequest.
For the communication between client and server, it is beneficial. Http Request’s communication is through GET and POST methods in which the client sends a request to a server encoded in the URL using the GET method or in the HTTP body using the POST method.
JsonRequest is an execution of client/server communication - It allows the performance of some method for the client on the server by passing some arguments to the method. Responses can get from method calls; as a result, the client gets some data.
class WebRequest(httprequest)
WebRequest is the Parent Class for every Odoo Web request type. We can see this class in the odoo/http.py file.
Parameters:httprequest-a wrapped werkzeug Request object
httprequest-The original class - ’werkzeug.wrappers.Request Params have request parameters, keyword arguments, and they will be passed to the handler method.
Properties:
cr
Call initializes the method call.’It is one of the concepts of SQL, which acts as a looping statement that fetches data row by row basis.
Exception raises while current request use a none authentication(auth=” none”) 
context
Mapping the context values for the current request
env
The Environment that is bound to the current request.
session
HTTP Session data of current HTTP session given by ERP session.
DB
The request is linked with the database. It can be None if authentication is none for the current request.
csrf_token(time_limit=None)- It is a random unique security token that is used to prevent CSRF attacks. It returns ASCII token string. iToken validity in seconds is given by time_limit (int | None), Token will be valid as long as the current user session is default and when it is None.
Class HttpRequest(*args) is the handler for HTTP requests.Keyword arguments of handler methods are Routing parameters, query string parameters, form parameters, and files 
Routing parameters will have priority if there is a name conflict
The handler method’s result can be:
- If the HTTP response is HTTP 204, which is ‘No Content,’ the result will be a false value.
- a werkzeug Response object
- The response object is interpreted as HTML with str or Unicode.
make_response(data, headers=None, cookies=None) - It acts as a helper for HTML and Non-HTML responses
Handlers can return the HTML markup of a page as a string. We have to create a complete response object when non-HTML data is returned, otherwise returned data will not be interpreted with the clients.
Parameters
- data (basestring) – response body
- headers ([(name, value)]) – HTTP headers for response
- cookies (collections.Mapping) – cookies set on client
not_found(description=None) - Shortcut for a HTTP 404 (Not Found) response
render(template, qcontext=None, lazy=True, **kw)
Parameters
- template(basestring)- rendering template
- qcontext - rendering context (dictionary format)
- lazy - If it is true, the rendering of the template should be deferred until the last moment(Bool format)
- kw - forwarded to the Response object
Class JsonRequest(*args)
Request handler for JSON-RPC 2 over HTTP.
method is disregarded
- params must be a JSON object (not an array, but dict)
- The result of the handler method is returning a JSON-RPC 
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)
Response object passed through controller route.
In addition to the  werkzeug.wrappers.Response parameters, the constructor of this class, can also take the following parameters for QWeb Lazy Rendering.
Parameters
- template (basestring) – the rendering template
- qcontext (dict) – context used for rendering. It is in dict format.
- uid (int) – User id to use for the ir.ui.view render call, the default for this parameter is None
these attributes can be changed at any time before rendering 
render()-Renders the template and returns result
flatten() - Forces the rendering of response template, sets the result as response body and unsets template


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



0
Comments



Leave a comment



whatsapp
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