I am making a python application that gets some custom fields from the sale.order model for an external tag server, the file was already working in a version of Odoo v12 but recently I am migrating this process to Odoo v15 , however, when sending the json that brings back the info in the fields sends the following error:
2022-08-30 22:23:02,868 16792 ERROR yuju odoo.http: Exception during JSON request handling.
Traceback (most recent call last):
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Odoo 15e\server\odoo\http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "C:\Odoo 15e\server\odoo\http.py", line 301, in _handle_exception
raise exception.with_traceback(None) from new_cause
werkzeug.exceptions.BadRequest:
400 Bad Request: <function Home.index at 0x000002F31ABDD318>, /: Function declared as capable of handling request of type 'http' but called with a request of type 'json'
This is the configuration of the program and the function that calls the info from odoo:
dir_path = os.path.dirname(os.path.realpath(__file__))
server_url ='http://localhost:8090/'
db_name = 'yuju'
username = 'odoo'
password = '1234'
json_endpoint = "%s/jsonrpc" % server_url
headers = {"Content-Type": "application/json"}
def get_json_payload(service, method, *args):
return json.dumps({
"jsonrpc": "2.0",
"method": 'call',
"params": {
"service": service,
"method": method,
"args": args
},
"id": 6,
})
user_id = 6
def get_order_id(name):
try:
payload = get_json_payload("common", "version")
response = requests.post(json_endpoint, data=payload, headers=headers)
if user_id:
search_domain = [['name', '=', name]]
payload = get_json_payload("object", "execute_kw",db_name, user_id, password,'sale.order', 'search_read', [search_domain, ['marketplace_order_id', 'name', 'seller_marketplace']],
{'limit': 1})
print(payload)
res = requests.post(json_endpoint, data=payload, headers=headers)#.json()
#logging.info(default_code+str(res))
print (res)
marketplace_order_id = res['result'][0]['marketplace_order_id']
print('marketplace_order_id', marketplace_order_id)
seller_marketplace = res['result'][0]['seller_marketplace']
order_odoo_id = res['result'][0]['id']
return dict(marketplace_order_id = marketplace_order_id, seller_marketplace =seller_marketplace, order_odoo_id = order_odoo_id )
else:
logging.error("Failed: wrong credentials")
return False
except Exception as e:
logging.error ('Error:'+str(e))
return False
Debugging I could see that it is right on the line that contains the following where the error is generated:
res = requests.post(json_endpoint, data=payload, headers=headers)#.json()
UPDATE
The previous error was due to the fact that the json_endpoint variable already has a slash and collided with the final slash of the server_url variable , I put the correction below:
server_url ='http://localhost:8090' #aquí estaba el fallo
json_endpoint = "%s/jsonrpc" % server_url
I also installed in odoo an application that comes by default called test_rpc for rpc and json-rpc requests as a python library
The error I have now is this:
Traceback (most recent call last):
File "C:\Odoo 15e\server\odoo\http.py", line 141, in dispatch_rpc
result = dispatch(method, params)
File "C:\Odoo 15e\server\odoo\service\model.py", line 41, in dispatch
res = fn(db, uid, *params)
File "C:\Odoo 15e\server\odoo\service\model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "C:\Odoo 15e\server\odoo\service\model.py", line 176, in execute
res = execute_cr(cr, uid, obj, method, *args, **kw)
File "C:\Odoo 15e\server\odoo\service\model.py", line 160, in execute_cr
result = odoo.api.call_kw(recs, method, args, kw)
File "C:\Odoo 15e\server\odoo\api.py", line 457, in call_kw
result = _call_kw_model(method, model, args, kwargs)
File "C:\Odoo 15e\server\odoo\api.py", line 430, in _call_kw_model
result = method(recs, *args, **kwargs)
File "C:\Odoo 15e\server\odoo\models.py", line 5012, in search_read
records = self.search(domain or [], offset=offset, limit=limit, order=order)
File "C:\Odoo 15e\server\odoo\models.py", line 1810, in search
res = self._search(args, offset=offset, limit=limit, order=order, count=count)
File "C:\Odoo 15e\server\odoo\models.py", line 4675, in _search
if expression.is_false(self, args):
File "C:\Odoo 15e\server\odoo\osv\expression.py", line 229, in is_false
elif token[1] == 'in' and not (isinstance(token[2], Query) or token[2]):
IndexError: tuple index out of range
Sorry to answer myself, but this is the (somewhat simple) solution to the problem I had.
Instead of generating a JSON with the get_json_payload function , I wrapped the JSON directly in the get_order_id function ,