yezyilomo/odoo-rest-api

404 NotFound

HamaniKhalil opened this issue · 3 comments

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
    }
}

Hello

  • Body - None (because first call /web/session/authenticate then after session_id included on all requests header)
  • Header - No content-type
  • Method - Get
  • Params - key => query, value=>{id,name}

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

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'