i'm trying to make a request to a search_read endpoint and the result is session expired, my code is this:
first i login to get the session_id
$data = array(
'json-rpc' => 2.0,
'method' => 'call',
'params' => array(
'db' => 'xxxx',
'login' => 'xxxx',
'password' => 'xxxx',
'context' => array()
),
'id' => 10
);
$data_string = json_encode($data);
$ch = curl_init('https://xxxxxxxxxxxxx/web/session/authenticate');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
)
);
$res = curl_exec($ch);
print_r(json_decode($res)->result->session_id);
response:
{"result": {"uid": 2, "partner_display_name": "ARISMEL", "username": "admin", "odoobot_initialized": true, "web.base.url": "http://xxxxxxxxxxxxxx.com.co", "user_companies": false, "max_time_between_keys_in_ms": 55, "show_effect": "True", "is_system": true, "server_version_info": [12, 0, 0, "final", 0, ""], "is_admin": true, "server_version": "12.0-20210728", "db": "lujosec", "user_context": {"uid": 2, "lang": "es_CO", "tz": "America/Bogota"}, "web_tours": [], "name": "ARISMEL", "partner_id": 3, "session_id": "d48914eb50a0bd286873a88fa660a9459008ee75", "company_id": 1, "currencies": {"8": {"position": "before", "symbol": "$", "digits": [69, 2]}, "1": {"position": "after", "symbol": "\u20ac", "digits": [69, 2]}, "2": {"position": "before", "symbol": "$", "digits": [69, 2]}}}, "jsonrpc": "2.0", "id": 10}
Then use the id_session in the header for the other request
$data = array(
'json-rpc' => 2.0,
'method' => 'call',
'params' => array(
'model' => 'res.users',
'fields' => ["id", "name", "login"],
'domain' => array(),
'context' => array(),
),
'id' => 10,
);
$data_string = json_encode($data);
$ch = curl_init('https://xxxxxxxxxxxxxxxxxxxxx/web/dataset/search_read');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'x-openerp-session_id : ' . json_decode($res)->result->session_id,
'Cookie: sessionid='. json_decode($res)->result->session_id ,
)
);
$res = curl_exec($ch);
print_r($res);
response:
{"jsonrpc": "2.0", "error": {"message": "Odoo Session Expired", "data": {"arguments": ["Session expired"], "exception_type": "internal_error", "message": "Session expired", "debug": "Traceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 656, in _handle_exception\n return super(JsonRequest, self)._handle_exception(exception)\n File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 314, in _handle_exception\n raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])\n File \"/usr/lib/python3/dist-packages/odoo/tools/pycompat.py\", line 87, in reraise\n raise value\n File \"/usr/lib/python3/dist-packages/odoo/addons/http_routing/models/ir_http.py\", line 352, in _dispatch\n cls._authenticate(func.routing['auth'])\n File \"/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_http.py\", line 118, in _authenticate\n getattr(cls, \"_auth_method_%s\" % auth_method)()\n File \"/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_http.py\", line 91, in _auth_method_user\n raise http.SessionExpiredException(\"Session expired\")\nodoo.http.SessionExpiredException: Session expired\n", "name": "odoo.http.SessionExpiredException"}, "code": 100}, "id": 10}
A curious fact is that in postman it works fine, it works without session id I don't understand it.
Odoo version 12. I was trying in javascript, using responses with fetch or axios but the result is the same,
please help, best regards