Availability |
Odoo Online
Odoo.sh
On Premise
|
Lines of code | 418 |
Technical Name |
restful |
License | LGPL-3 |
Website | https://ajepe.github.io/blog/restful-api-for-odoo |
Versions | 14.0 |
Availability |
Odoo Online
Odoo.sh
On Premise
|
Lines of code | 418 |
Technical Name |
restful |
License | LGPL-3 |
Website | https://ajepe.github.io/blog/restful-api-for-odoo |
Versions | 14.0 |
Odoo RESTFUL API
This module helps you to connect or integrate any third party application with Odoo.
It provides a fluent and easy HTTP based API interface instead of the Odoo default RPC.
By design Odoo is shipped with RPC API interfaces which may not be straight forward for many developers that are not familiar with the RPC(s).
This is module only cares about generating an HTTP response for each HTTP request, it has nothing to do with your or existing application logic.
For detailed example of REST API refer
https://ajepe.github.io/posts/restful-api-for-odoo
Please log in to comment on this module
Report comment
Any abuse of this reporting system will be penalizedException
When i try to connect Traceback (most recent call last): File "testapi.py", line 20, in content = json.loads(req.content.decode('utf-8')) File "C:\Users\ThinkMagine\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads return _default_decoder.decode(s) File "C:\Users\ThinkMagine\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\ThinkMagine\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I don't Know
I have win10 and Odoo12 on it, I can't found your app on the app store on Odoo, I tried to download your app/ put it into C:\Program Files (x86)\Odoo 12.0\server\addons /restart my Odoo server and activate developer mode but nothing happens.
Fix this issue please
I followed your documentation, and am using your app on a Odoo 10 version. When I try a GET request I have this response: { "jsonrpc": "2.0", "id": null, "error": { "message": "404: Not Found", "code": 404, "data": { "debug": "Traceback (most recent call last):\n File \"/usr/lib/python2.7/dist-packages/odoo/http.py\", line 642, in _handle_exception\n return super(JsonRequest, self)._handle_exception(exception)\n File \"/usr/lib/python2.7/dist-packages/odoo/addons/base/ir/ir_http.py\", line 177, in _dispatch\n rule, arguments = cls._find_handler(return_rule=True)\n File \"/usr/lib/python2.7/dist-packages/odoo/addons/base/ir/ir_http.py\", line 79, in _find_handler\n return cls.routing_map().bind_to_environ(request.httprequest.environ).match(return_rule=return_rule)\n File \"/usr/lib/python2.7/dist-packages/werkzeug/routing.py\", line 1430, in match\n raise NotFound()\nNotFound: 404: Not Found\n", "exception_type": "internal_error", "message": "404: Not Found", "name": "werkzeug.exceptions.NotFound", "arguments": [] }, "http_status": 404 } } Please help me with that
Re: Fix this issue please
I don't update or support version below 12.0 anymore but seems you are passing wrong values as part of your request and also using version 12.0 on your verson10 installation.
Very good app. Thanks for providing this app
Would like to try this
Attempting to install the plugin on ODOO 12.0. It fails, and I see this error: Odoo Server Error Traceback (most recent call last): File "/home/regrehan/odoo-dev/odoo/odoo/models.py", line 1108, in _validate_fields check(self) File "/home/regrehan/odoo-dev/odoo/odoo/addons/base/models/ir_ui_view.py", line 351, in _check_xml self.postprocess_and_fields(view.model, view_doc, view.id) File "/home/regrehan/odoo-dev/odoo/odoo/addons/base/models/ir_ui_view.py", line 1106, in postprocess_and_fields self.raise_view_error(message, view_id) File "/home/regrehan/odoo-dev/odoo/odoo/addons/base/models/ir_ui_view.py", line 548, in raise_view_error raise ValueError(message) ValueError: Field `rest_api` does not exist There is more. It all appears to have to do with the Field 'rest_api' does not exist.
Having Issues
Hello Ajepe, I'm having an issue when attempting to read a model. url = "https://url/api/sale.order" headers = { 'access-token': "access_token_43d090f52c9aaa72a73480e0bd1b06530d0c933c", } response = requests.request("GET", url, headers=headers) odoo log says: File "/home/odoo/src/user/ajepe/odoo-addons/restful/controllers/main.py", line 92, in get return valid_response(data) File "/home/odoo/src/user/ajepe/odoo-addons/restful/common.py", line 21, in valid_response response=json.dumps(data), File "/home/odoo/.local/lib/python3.6/site-packages/simplejson/__init__.py", line 382, in dumps return _default_encoder.encode(obj) File "/home/odoo/.local/lib/python3.6/site-packages/simplejson/encoder.py", line 296, in encode chunks = self.iterencode(o, _one_shot=True) File "/home/odoo/.local/lib/python3.6/site-packages/simplejson/encoder.py", line 378, in iterencode return _iterencode(o, 0) File "/home/odoo/.local/lib/python3.6/site-packages/simplejson/encoder.py", line 273, in default o.__class__.__name__) TypeError: Object of type datetime is not JSON serializable - - - This occurs when attempting to read any model. Is there any fix for this?
Re: Having Issues
What version are you referring to? I guess it will version 12.0.
The database name is not valid Access denied
fake addon
Doesn't work neither reading nor creating
Fix this error please
I just made Read [GET] request in python exactly from your docs in new database with demo data and I got this error: 2018-11-15 13:13:03,595 12853 INFO testmoham odoo.addons.base.models.res_users: Login successful for db:testmoham login:admin from 127.0.0.1 2018-11-15 13:13:03,632 12853 INFO testmoham werkzeug: 127.0.0.1 - - [15/Nov/2018 13:13:03] "GET /api/auth/token HTTP/1.1" 200 - 45 0.019 0.068 2018-11-15 13:13:03,746 12853 INFO testmoham werkzeug: 127.0.0.1 - - [15/Nov/2018 13:13:03] "GET /api/sale.order/ HTTP/1.1" 500 - 76 0.038 0.073 2018-11-15 13:13:03,755 12853 ERROR testmoham werkzeug: Error on request: Traceback (most recent call last): File "/usr/lib/python3.6/site-packages/werkzeug/serving.py", line 209, in run_wsgi execute(self.server.app) File "/usr/lib/python3.6/site-packages/werkzeug/serving.py", line 197, in execute application_iter = app(environ, start_response) File "/home/odoo/odoo12/odoo/service/server.py", line 255, in app return self.app(e, s) File "/home/odoo/odoo12/odoo/service/wsgi_server.py", line 128, in application return application_unproxied(environ, start_response) File "/home/odoo/odoo12/odoo/service/wsgi_server.py", line 117, in application_unproxied result = odoo.http.root(environ, start_response) File "/home/odoo/odoo12/odoo/http.py", line 1317, in __call__ return self.dispatch(environ, start_response) File "/home/odoo/odoo12/odoo/http.py", line 1290, in __call__ return self.app(environ, start_wrapped) File "/usr/lib/python3.6/site-packages/werkzeug/wsgi.py", line 600, in __call__ return self.app(environ, start_response) File "/home/odoo/odoo12/odoo/http.py", line 1485, in dispatch result = ir_http._dispatch() File "/home/odoo/odoo12/addons/auth_signup/models/ir_http.py", line 19, in _dispatch return super(Http, cls)._dispatch() File "/home/odoo/odoo12/addons/web_editor/models/ir_http.py", line 22, in _dispatch return super(IrHttp, cls)._dispatch() File "/home/odoo/odoo12/addons/http_routing/models/ir_http.py", line 394, in _dispatch result = super(IrHttp, cls)._dispatch() File "/home/odoo/odoo12/addons/utm/models/ir_http.py", line 26, in _dispatch response = super(IrHttp, cls)._dispatch() File "/home/odoo/odoo12/odoo/addons/base/models/ir_http.py", line 207, in _dispatch return cls._handle_exception(e) File "/home/odoo/odoo12/addons/utm/models/ir_http.py", line 31, in _handle_exception response = super(IrHttp, cls)._handle_exception(exc) File "/home/odoo/odoo12/odoo/addons/base/models/ir_http.py", line 177, in _handle_exception return request._handle_exception(exception) File "/home/odoo/odoo12/odoo/http.py", line 773, in _handle_exception return super(HttpRequest, self)._handle_exception(exception) File "/home/odoo/odoo12/odoo/http.py", line 312, in _handle_exception raise pycompat.reraise(type(exception), exception, sys.exc_info()[2]) File "/home/odoo/odoo12/odoo/tools/pycompat.py", line 87, in reraise raise value File "/home/odoo/odoo12/odoo/addons/base/models/ir_http.py", line 203, in _dispatch result = request.dispatch() File "/home/odoo/odoo12/odoo/http.py", line 832, in dispatch r = self._call_function(**self.params) File "/home/odoo/odoo12/odoo/http.py", line 344, in _call_function return checked_call(self.db, *args, **kwargs) File "/home/odoo/odoo12/odoo/service/model.py", line 97, in wrapper return f(dbname, *args, **kwargs) File "/home/odoo/odoo12/odoo/http.py", line 337, in checked_call result = self.endpoint(*a, **kw) File "/home/odoo/odoo12/odoo/http.py", line 938, in __call__ return self.method(*args, **kw) File "/home/odoo/addons12/restful/controllers/main.py", line 28, in wrap return func(self, *args, **kwargs) File "/home/odoo/odoo12/odoo/http.py", line 517, in response_wrap response = f(*args, **kw) File "/home/odoo/addons12/restful/controllers/main.py", line 55, in get domain=domain, fields=fields, offset=offset, limit=limit, order=order) File "/home/odoo/odoo12/odoo/models.py", line 4516, in search_read result = records.read(fields) File "/home/odoo/odoo12/odoo/models.py", line 2768, in read values[name] = field.convert_to_read(record[name], record, use_name_get) File "/home/odoo/odoo12/odoo/models.py", line 5042, in __getitem__ return self._fields[key].__get__(self, type(self)) File "/home/odoo/odoo12/odoo/fields.py", line 974, in __get__ self.determine_value(record) File "/home/odoo/odoo12/odoo/fields.py", line 1085, in determine_value self.compute_value(recs) File "/home/odoo/odoo12/odoo/fields.py", line 1041, in compute_value self._compute_value(records) File "/home/odoo/odoo12/odoo/fields.py", line 1032, in _compute_value getattr(records, self.compute)() File "/home/odoo/odoo12/addons/sale/models/sale.py", line 262, in _compute_type_name record.type_name = _('Quotation') if self.state in ('draft', 'sent', 'cancel') else _('Sales Order') File "/home/odoo/odoo12/odoo/fields.py", line 968, in __get__ record.ensure_one() File "/home/odoo/odoo12/odoo/models.py", line 4643, in ensure_one raise ValueError("Expected singleton: %s" % self) ValueError: Expected singleton: sale.order(8, 3, 4, 6, 7, 9, 10, 14, 15, 16) - - -
Re: Fix this error please
The error is actually coming from your code
I don't Know
I have win10 and Odoo12 on it, I can't found your app on the app store on Odoo, I tried to download your app/ put it into C:\Program Files (x86)\Odoo 12.0\server\addons /restart my Odoo server and activate developer mode but nothing happens.
data field doesn't work (limit, domain, etc).
I have followed your exact code from your Github post and ran it. I can authenticate perfectly, but when I do a search read, I always get all database lines from that particular model in return. The limit field doesn't work, etc. Odoo 12 Enterprise. What to do?
Issues with the put requests
When I'm doing a PUT request is having an issue because all the fields are taken as strings and we have this output Database fetch misses ids (('2',)) and has extra ids ((2,)), may be caused by a type incoherence in a previous request"}
Traceback (most recent call last): File "/home/surendra/isha/sushumna/odoo/odoo/service/wsgi_server.py", line 142, in application return application_unproxied(environ, start_response) File "/home/surendra/isha/sushumna/odoo/odoo/service/wsgi_server.py", line 117, in application_unproxied result = odoo.http.root(environ, start_response) File "/home/surendra/isha/sushumna/odoo/odoo/http.py", line 1281, in __call__ return self.dispatch(environ, start_response) File "/home/surendra/isha/sushumna/odoo/odoo/http.py", line 1251, in __call__ return self.app(environ, start_wrapped) File "/home/surendra/isha/sushumna/env/lib/python3.7/site-packages/werkzeug/wsgi.py", line 766, in __call__ return self.app(environ, start_response) File "/home/surendra/isha/sushumna/odoo/odoo/http.py", line 1450, in dispatch result = ir_http._dispatch() File "/home/surendra/isha/sushumna/odoo/addons/auth_signup/models/ir_http.py", line 19, in _dispatch return super(Http, cls)._dispatch() File "/home/surendra/isha/sushumna/odoo/addons/web_editor/models/ir_http.py", line 22, in _dispatch return super(IrHttp, cls)._dispatch() File "/home/surendra/isha/sushumna/odoo/addons/http_routing/models/ir_http.py", line 516, in _dispatch result = super(IrHttp, cls)._dispatch() File "/home/surendra/isha/sushumna/odoo/odoo/addons/base/models/ir_http.py", line 233, in _dispatch return cls._handle_exception(e) File "/home/surendra/isha/sushumna/odoo/addons/http_routing/models/ir_http.py", line 607, in _handle_exception return super(IrHttp, cls)._handle_exception(exception) File "/home/surendra/isha/sushumna/odoo/odoo/addons/base/models/ir_http.py", line 198, in _handle_exception raise exception File "/home/surendra/isha/sushumna/odoo/odoo/addons/base/models/ir_http.py", line 229, in _dispatch result = request.dispatch() File "/home/surendra/isha/sushumna/odoo/odoo/http.py", line 803, in dispatch r = self._call_function(**self.params) File "/home/surendra/isha/sushumna/odoo/odoo/http.py", line 345, in _call_function return checked_call(self.db, *args, **kwargs) File "/home/surendra/isha/sushumna/odoo/odoo/service/model.py", line 93, in wrapper return f(dbname, *args, **kwargs) File "/home/surendra/isha/sushumna/odoo/odoo/http.py", line 338, in checked_call result = self.endpoint(*a, **kw) File "/home/surendra/isha/sushumna/odoo/odoo/http.py", line 909, in __call__ return self.method(*args, **kw) File "/home/surendra/isha/sushumna/odoo/odoo/http.py", line 510, in response_wrap response = f(*args, **kw) File "/home/surendra/isha/sushumna/custom_addons/restful/controllers/token.py", line 94, in token access_token = _token.find_one_or_create_token(user_id=uid, create=True) File "/home/surendra/isha/sushumna/custom_addons/restful/models/access_token.py", line 50, in find_one_or_create_token access_token = self.env["api.access_token"].sudo().create(vals) File "", line 2, in create File "/home/surendra/isha/sushumna/odoo/odoo/api.py", line 343, in _model_create_multi return create(self, [arg]) File "/home/surendra/isha/sushumna/odoo/odoo/models.py", line 3666, in create vals = self._add_missing_default_values(vals) File "/home/surendra/isha/sushumna/odoo/odoo/models.py", line 1731, in _add_missing_default_values defaults = self.default_get(list(missing_defaults)) File "/home/surendra/isha/sushumna/odoo/odoo/models.py", line 1179, in default_get ir_defaults = self.env['ir.default'].get_model_defaults(self._name) File "", line 2, in get_model_defaults File "/home/surendra/isha/sushumna/odoo/odoo/tools/cache.py", line 90, in lookup value = d[key] = self.method(*args, **kwargs) File "/home/surendra/isha/sushumna/odoo/odoo/addons/base/models/ir_default.py", line 144, in get_model_defaults cr.execute(query, params) File "/home/surendra/isha/sushumna/odoo/odoo/sql_db.py", line 163, in wrapper return f(self, *args, **kwargs) File "/home/surendra/isha/sushumna/odoo/odoo/sql_db.py", line 240, in execute res = self._obj.execute(query, params) psycopg2.ProgrammingError: operator does not exist: integer = boolean LINE 6: ... AND (d.company_id IS NULL OR d.company_id=false) ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
Error while calling the api to get a token
Hi, I'm getting the following error while getting the token using the below api. curl -X GET http://localhost:8069/api/auth/token -H 'cache-control: no-cache' -H 'charset: utf-8' -H 'content-type: application/x-www-form-urlencoded' -H 'postman-token: 98010ca0-2f81-52e5-91c1-3985761d9903' -d 'login=admin&password=admin&db=odoo' It is internally generating a bad SQL query like the below. Any help is appreciated. 2019-12-28 10:40:40,553 20407 ERROR odoo odoo.sql_db: bad query: SELECT f.name, d.json_value FROM ir_default d JOIN ir_model_fields f ON d.field_id=f.id WHERE f.model='api.access_token' AND (d.user_id IS NULL OR d.user_id=NULL) AND (d.company_id IS NULL OR d.company_id=false) AND d.condition IS NULL ORDER BY d.user_id, d.company_id, d.id Full stack trace below.
mail.message model POST request show 401UNAUTHORIZED.
mail.message model POST request show 401UNAUTHORIZED. I send access-token correctly. so, I am not able to post message via mail.message model.
i have problem for Call GET
hi bro @ajepe how to fixed { "jsonrpc": "2.0", "id": null, "error": { "code": 200, "message": "Odoo Server Error", "data": { "name": "werkzeug.exceptions.BadRequest", "debug": "Traceback (most recent call last):\n File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 653, in _handle_exception\n return super(JsonRequest, self)._handle_exception(exception)\n File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 312, 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/http.py\", line 695, in dispatch\n result = self._call_function(**self.params)\n File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 320, in _call_function\n raise werkzeug.exceptions.BadRequest(msg % params)\nwerkzeug.exceptions.BadRequest: 400 Bad Request: , /api/res.partner/: Function declared as capable of handling request of type 'http' but called with a request of type 'json'\n", "message": "400 Bad Request: , /api/res.partner/: Function declared as capable of handling request of type 'http' but called with a request of type 'json'", "arguments": [], "exception_type": "internal_error" } } } i'm use odoo 11 and testing use postman, only GET cant call, parameter json { "limit" : 10, "domain":[ { }, { } ], "order":"name asc", "offset":"10" } please help me bro
can not get image in product.template database.
I used this model in odoo12 enterprise version, in the "/api/product.template" API, I always got the "image": null, "image_medium": null, "image_small": null,
Issue with *2many on create
Hello Ajepe, I have an issue with *2many fields when I create new records. I used the same example in the module documentation. """Create a new record. Basic sage: import requests headers = { 'content-type': 'application/x-www-form-urlencoded', 'charset': 'utf-8', 'access-token': 'access_token' } data = { 'name': 'Babatope Ajepe', 'country_id': 105, 'child_ids': [ { 'name': 'Contact', 'type': 'contact' }, { 'name': 'Invoice', 'type': 'invoice' } ], 'category_id': [{'id': 9}, {'id': 10}] } req = requests.post('%s/api/res.partner/' % base_url, headers=headers, data=data) """ The record is created but child_ids and category_id are not there.
have you an example with java code please that use restfull api
who can help me how to use this modul .. some steps useful .. thank you
Re:
Check out the documentation here https://ajepe.github.io/posts/restful-api-for-odoo/
Error on GET crm.lead
req = requests.get('{}/api/crm.lead'.format(base_url), headers=headers, data={'limit': 1, 'domain': []}) ``` File "/media/ricardo/Dados/Codidos/Python/odoo/custom_addons/restful/common.py", line 22, in valid_response response=json.dumps(data, default=datetime.datetime.isoformat), File "/media/ricardo/Dados/Codidos/Python/odoo/venv/lib/python3.5/site-packages/simplejson/__init__.py", line 399, in dumps **kw).encode(obj) File "/media/ricardo/Dados/Codidos/Python/odoo/venv/lib/python3.5/site-packages/simplejson/encoder.py", line 296, in encode chunks = self.iterencode(o, _one_shot=True) File "/media/ricardo/Dados/Codidos/Python/odoo/venv/lib/python3.5/site-packages/simplejson/encoder.py", line 378, in iterencode return _iterencode(o, 0) TypeError: descriptor 'isoformat' requires a 'datetime.datetime' object but received a 'datetime.date' - - - ```
@Betty
Object of type datetime is not JSON serializable error is now fixed
Error after request
Hello Ajepe, I got an error after url/api/res.partner search: File "/odoo/custom/addons/restful/controllers/main.py", line 26, in wrap "access_token_not_found", "missing access token in request header", 401 File "/odoo/custom/addons/restful/common.py", line 33, in invalid_response response=json.dumps( NameError: name 'json' is not defined - - - Please help to sort this issue? My regards.
Re: Error after request
You need to always send access token with every request check https://github.com/ajepe/odoo-addons/tree/12.0/restful for how to send correct request.
Thanks it work
This is work for me
the answer
Here is the answer for the previous issue: https://stackoverflow.com/questions/45466814/odoo-url-not-found-if-not-auth Regards.
not working if no session_id is specified
Hi, (checking this https://ajepe.github.io/posts/restful-api-for-odoo/). It throws 404 if not session_id is specified. What is the correct way to connect without needing to include sessino_id? Thank you.
404 Not Foujd
Hi, good module. I'm trying to test this module but it never connects to the url. It only connects if I include 'Cookie': 'session_id=a40de38883ddbf04ab9d070ab950d0216f710a67'. What is the correct way to use this module, I don't want to spedify a session_id into the header. Thank you.
@ibyref and @Otto Kalme Can you describe what you are trying to achieve that is giving you an error with the code snippets with that I should be able to help you guys.