This question has been flagged

Odoo is having capability to handle only one request type from the controller(json or http).


The scenario is third party api is calling the odoo controller wiht HTTP request to verify the URL(odoo controller route). But after verify that URL that third party api is POST some data to that same URL(odoo controller route) with JSON request.


So I want to handle the HTTP and JSON request with the same controller.


Please guide me to solve this.


Thanks !

Avatar
Discard
Best Answer

In Odoo 14

Import the WebRequest and overwrite the _callfunction method, add the script to your module, include in the init file


# -*- coding: utf-8 -*-
from odoo.http import WebRequest, Response
import logging
from odoo.service import security, model as service_model
from odoo.sql_db import flush_env

_logger = logging.getLogger(__name__)


def _call_function(self, *args, **kwargs):
request = self
if self.endpoint.routing['type'] != self._request_type:
msg = "%s, %s: Function declared as capable of handling request of type '%s' but called with a request of type '%s'"
params = (self.endpoint.original, self.httprequest.path, self.endpoint.routing['type'], self._request_type)
_logger.info(msg, *params)
# raise werkzeug.exceptions.BadRequest(msg % params)

if self.endpoint_arguments:
kwargs.update(self.endpoint_arguments)

# Backward for 7.0
if self.endpoint.first_arg_is_req:
args = (request,) + args

first_time = True

# Correct exception handling and concurency retry
@service_model.check
def checked_call(___dbname, *a, **kw):
nonlocal first_time
# The decorator can call us more than once if there is an database error. In this
# case, the request cursor is unusable. Rollback transaction to create a new one.
if self._cr and not first_time:
self._cr.rollback()
self.env.clear()
first_time = False
result = self.endpoint(*a, **kw)
if isinstance(result, Response) and result.is_qweb:
# Early rendering of lazy responses to benefit from @service_model.check protection
result.flatten()
if self._cr is not None:
# flush here to avoid triggering a serialization error outside
# of this context, which would not retry the call
flush_env(self._cr)
self._cr.precommit.run()
return result

if self.db:
return checked_call(self.db, *args, **kwargs)
return self.endpoint(*args, **kwargs)

setattr(WebRequest, '_call_function', _call_function)

Avatar
Discard