I am working on odoo 17 community and I am trying to create api json files to connect to for example contacts module I created controller as below but when I try to test the following link on the browser I am getting error:
<code>http://localhost:8069/api/contacts
from odoo import http
</code>
<code>http://localhost:8069/api/contacts
from odoo import http
</code>
http://localhost:8069/api/contacts
from odoo import http
from odoo.http import request
class ContactController(http.Controller):
<code>@http.route('/api/contacts', type='json', auth='public', methods=['POST'])
def get_contacts(self):
try:
# Log the incoming request data for debugging
print("Request data:", request.jsonrequest)
# Handle the request and return the contact list
contacts = request.env['res.partner'].search([('is_company', '=', False)])
contact_list = []
for contact in contacts:
contact_list.append({
'id': contact.id,
'name': contact.name,
'email': contact.email,
'phone': contact.phone,
'street': contact.street,
'city': contact.city,
'state': contact.state_id.name,
'country': contact.country_id.name
})
return contact_list
except Exception as e:
# Log the exception for debugging
print("Error:", str(e))
return {'error': str(e)}
</code>
<code>@http.route('/api/contacts', type='json', auth='public', methods=['POST'])
def get_contacts(self):
try:
# Log the incoming request data for debugging
print("Request data:", request.jsonrequest)
# Handle the request and return the contact list
contacts = request.env['res.partner'].search([('is_company', '=', False)])
contact_list = []
for contact in contacts:
contact_list.append({
'id': contact.id,
'name': contact.name,
'email': contact.email,
'phone': contact.phone,
'street': contact.street,
'city': contact.city,
'state': contact.state_id.name,
'country': contact.country_id.name
})
return contact_list
except Exception as e:
# Log the exception for debugging
print("Error:", str(e))
return {'error': str(e)}
</code>
@http.route('/api/contacts', type='json', auth='public', methods=['POST'])
def get_contacts(self):
try:
# Log the incoming request data for debugging
print("Request data:", request.jsonrequest)
# Handle the request and return the contact list
contacts = request.env['res.partner'].search([('is_company', '=', False)])
contact_list = []
for contact in contacts:
contact_list.append({
'id': contact.id,
'name': contact.name,
'email': contact.email,
'phone': contact.phone,
'street': contact.street,
'city': contact.city,
'state': contact.state_id.name,
'country': contact.country_id.name
})
return contact_list
except Exception as e:
# Log the exception for debugging
print("Error:", str(e))
return {'error': str(e)}