Skip to Content
Menu

Json Request/Response Handler

by
Odoo

25.64

v 15.0 Third Party 2
Availability
Odoo Online
Odoo.sh
On Premise
Lines of code 29
Technical Name as_odoo_json_request_response_handler
LicenseOPL-1
Websitehttps://aemal-shirzai.github.io/portfolio
Versions 14.0 15.0
You bought this module and need support? Click here!
Availability
Odoo Online
Odoo.sh
On Premise
Lines of code 29
Technical Name as_odoo_json_request_response_handler
LicenseOPL-1
Websitehttps://aemal-shirzai.github.io/portfolio
Versions 14.0 15.0

Set Status Code and Headers in Response for JSON Request in Odoo.

By default there is no option to set response status code for json routes in odoo and this module allows developers to set status codes and headers for json routes in odoo.

Funtionalities:
  • By Default there is no option in odoo to set response status codes in response for JSON request, the status code is always 200 OK. But odoo provide those options for routes with type http.
  • This module allows developers to set all response status codes in response for json request.
  • It also allows developers to set custom headers in response for json request.

Problem Description:

This part describs the problem related to odoo json request and response:

Step 1: Create Routes and Return Response:

In odoo when we create a route with type json then there is not option to return custom status codes and headers in response of a json request. The status code is always 200 and can not add to headers. Odoo do provide mechanisms to change the response status code and headers with HTTP requests but not with JSON.
In HTTP requests you can use either Response.status or make_http_response() to change the status codes and headers but they can not be used with json.


Step 2: Send Requst to route.
  • Make Json request to the route we just created.
  • You will see that the response body and status code recieved are same as they were sent from odoo controller.

Step 3: Looking at odoo server
  • After that odoo server return 401 response for postman's request.
  • So after refresh or clicking new menu in odoo, you will see the 'connection lost, Trying to reconnect...' warning box at right top corner in odoo.
  • And nothing will work. Thats all because Response.status will make each request 401 after it return once 401.

Solution:

This part describs how this module works and how it solves the above problem:

Step 1: Create Routes and Return Response:

After installing the module, every time you want to return response to json request you just need to add status code to json_status_code key. and headers dictionary to json_headers key.


Step 2: Send Request to route.
  • Make Json request to the route we just created.
  • You will see that the response body and status code recieved are same as they were sent from odoo controller.

Step 3: Looking at odoo server
  • Although odoo server return 401 response for postman's request. The other internal odoo requests works fine and do not return 401
  • So after refresh or clicking new menu in odoo, you will NOT see the 'connection lost, Trying to reconnect...' warning box at right top corner in odoo.

Help and Support

Please feel free to contact us if you need technical or functional support, also you can find more of our excellent apps in the Odoo store.


Email Portfolio
Odoo Proprietary License v1.0

This software and associated files (the "Software") may only be used (executed,
modified, executed after modifications) if you have purchased a valid license
from the authors, typically via Odoo Apps, or if you have received a written
agreement from the authors of the Software (see the COPYRIGHT file).

You may develop Odoo modules that use the Software as a library (typically
by depending on it, importing it and using its resources), but without copying
any source code or material from the Software. You may distribute those
modules under the license of your choice, provided that this license is
compatible with the terms of the Odoo Proprietary License (For example:
LGPL, MIT, or proprietary licenses similar to this one).

It is forbidden to publish, distribute, sublicense, or sell copies of the Software
or modified copies of the Software.

The above copyright notice and this permission notice must be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Please log in to comment on this module

  • The author can leave a single reply to each comment.
  • This section is meant to ask simple questions or leave a rating. Every report of a problem experienced while using the module should be addressed to the author directly (refer to the following point).
  • If you want to start a discussion with the author or have a question related to your purchase, please use the support page.