from odoo import http
from odoo.http import request, Response
import json
class CustomAPIController(http.Controller):
@http.route('/custom_api/get_data', type='http', auth='public', methods=['GET'])
def get_data(self, **kw):
# Kiểm tra AccessToken từ HTTP headers
access_token = request.httprequest.headers.get('AccessToken')
if not access_token:
return Response(json.dumps({'error': 'Missing AccessToken'}),
content_type='application/json;charset=utf-8', status=401)
# Kiểm tra AccessToken có hợp lệ?
user_id = request.env["res.users.apikeys"]._check_credentials(
scope="rpc", key=access_token
)
if not user_id:
return Response(json.dumps({'error': 'API key invalid'}),
content_type='application/json;charset=utf-8', status=402)
# Xử lý logic lấy dữ liệu từ Odoo database
res_partner = request.env['res.partner'].sudo().search([])
partners_data = [{'id': partner.id, 'name': partner.name} for partner in res_partner]
return Response(json.dumps(partners_data), content_type='application/json;charset=utf-8')
.
Get data
curl - H
"AccessToken: your_secret_access_token"
https://your_odoo_instance/custom_api/get_data
Create accesstoken
Activate debug mode