Open HamaniKhalil opened 4 years ago
Hello
First Request - Web/session/authenticate
Second Request - GET /api/res.users
If you want to use /auth/
route for authentication this examples might help
import json
import requests
AUTH_URL = 'http://localhost:8069/auth/'
headers = {'Content-type': 'application/json'}
# Remember to configure default db on odoo configuration file(dbfilter = ^db_name$)
# Authentication credentials
data = {
'params': {
'login': 'your@email.com',
'password': 'yor_password',
'db': 'your_db_name'
}
}
# Authenticate user
res = requests.post(
AUTH_URL,
data=json.dumps(data),
headers=headers
)
# Get session_id from the response
# We are going to use this as our API key
session_id = json.loads(res.text)['result']['session_id']
# Example 1
# Get users
USERS_URL = 'http://localhost:8069/api/res.users/'
# Pass session_id for auth
# This will take time since it retrives all res.users fields
# You can use query param to fetch specific fields
params = {'session_id': session_id}
res = requests.get(
USERS_URL,
params=params
)
# This will be a very long response since it has many data
print(res.text)
# Example 2
# Get products(assuming you have products in you db)
# Here am using query param to fetch only product id and name(This will be faster)
USERS_URL = 'http://localhost:8069/api/product.product/'
# Pass session_id for auth
params = {'session_id': session_id, 'query': '{id, name}'}
res = requests.get(
USERS_URL,
params=params
)
# This will be small since we've retrieved only id and name
print(res.text)
@yezyilomo this example not working with odoo version 10 I get this error:
Traceback (most recent call last): File "auth.py", line 28, in
session_id = json.loads(res.text)['result']['session_id'] KeyError: 'result'
I've tried to GET from
res.users
using postman, and here's my request :GET /api/res.users
Request Body(I tried using only the Cookie header but it didn't work so I sent the session_id as a parameter as mentioned in the documentation)
Response