yezyilomo / odoo-rest-api

Module which expose Odoo as a REST API
MIT License
202 stars 162 forks source link

404 NotFound #32

Open HamaniKhalil opened 4 years ago

HamaniKhalil commented 4 years ago

I've tried to GET from res.users using postman, and here's my request :

GET /api/res.users Request Body

{
    "params": {
        "session_id": "19c6f61a6680310ad9aef098a1fdbc45c5d6f388"
    }
}

(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

{
    "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
    }
}
altanmur commented 4 years ago

Hello

First Request - Web/session/authenticate

Screen Shot 2020-04-22 at 07 34 18

Second Request - GET /api/res.users

Screen Shot 2020-04-22 at 07 34 26
yezyilomo commented 4 years ago

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)
m-abdalrahman commented 4 years ago

@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'