This module creates WebService frameworks to be used globally.
It introduces support for HTTP Request protocol. The webservice HTTP
call returns by default the content of the response. A content_only
parameter can be passed to get the full response object.
This addon is a simplified version of the webservice module. In most
cases, you can install webservice_base instead of webservice as
a drop-in replacement when you need to define a backend and perform HTTP
requests from server-side code.
It comes with no additional dependencies. For the original
implementation relying on component objects, see the webservice
addon module.
This work is derived from that module, removing the component and
server_env dependencies to make it lighter. As a design decision,
using component seems to an an additional abstraction layer (and
dependency) that brings little benefit, and removing it brings the
implementation closer to Odoo's framework design patterns.
Main differences with webservice:
webservice_basefocuses on direct usage from Odoo models (no component registry layer).- It provides a lightweight configuration model
(
webservice.backend) and a request adapter (webservice.request.adapter). - Features that depend on
server_environmentand component-based extensibility are not part ofwebservice_base.
Table of contents
To configure webservice_base:
- Enable developer mode.
- Go to Settings > Technical > WebService Backend.
- Create a backend record:
- Protocol: select
HTTP Request - URL: base URL of the remote service, for example
https://api.example.com/ - Content Type: set a default
Content-Typeheader (optional) - Authentication:
- Public: no authentication
- Username & password: set
UsernameandPassword - API Key: set
API Key header(for exampleAuthorizationorX-Api-Key) andAPI Key
- Protocol: select
- (Optional) Tune timeouts:
Connect timeout: seconds to establish the TCP connectionRead timeout: seconds to wait for the response
The backend record can be used directly from server-side code using the
call() helper or the inherited adapter methods (get, post,
put).
This module is meant to be used from server-side code.
- Create a
webservice.backendrecord (see Configuration). - From Python code, fetch the backend and call HTTP methods.
backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
result = backend.call("get", url="api/v1/ping")- If
urlis relative, it is joined with the backendurl. - If
urlis an absolute URL, it is used as-is.
import json
backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
payload = {"name": "John"}
result = backend.call(
"post",
url="api/v1/contacts",
data=json.dumps(payload),
headers={"Accept": "application/json"},
content_type="application/json",
)By default, calls return response.content. To get the full
requests.Response, set content_only to False:
backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
response = backend.call("get", url="api/v1/ping", content_only=False)
status_code = response.status_code
body = response.contentYou can override timeouts per call using timeout (as accepted by
requests):
- A single number applies to both the connect and read timeouts.
- A tuple
(connect_timeout, read_timeout)allows setting them separately.
backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
result = backend.call("get", url="api/v1/ping", timeout=(2, 10))If your backend URL or endpoint contains format placeholders, pass
url_params:
backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
backend.url = "https://api.example.com/{version}/"
result = backend.call("get", url="resources/{resource_id}", url_params={"version": "v1", "resource_id": 42})Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed feedback.
Do not contact contributors directly about support or help with technical issues.
- Creu Blanca
- Camptocamp
- Enric Tobella <etobella@creublanca.es>
- Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
- Daniel Reis <dreis@opensourceintegrators.com>
This module is maintained by the OCA.
OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.
Current maintainer:
This module is part of the OCA/web-api project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.





