Skip to Content
Menú
This question has been flagged
2 Respostes
391 Vistes

Hey guys please help. I have created a controller in odoo17 and when try to POST a request using postman I am getting the error below.

2025-06-23 08:15:22,702 18051 INFO ? werkzeug: 127.0.0.1 - - [23/Jun/2025 08:15:22] "POST /api/student/create HTTP/1.1" 404 - 1 0.003 0.042



I have also imported my file in the __init__.py

from . import student_api_controller

Here is the url I am using in postman
http://localhost:8069/api/student/create

Here is my controller 

from odoo import http
from odoo.http import request
import json
import urllib
import logging

_logger = logging.getLogger(__name__)


class StudentAPI(http.Controller):

@http.route('/api/student/create', type='http', auth='public', methods=['POST'], csrf=False)
def create_student(self, **kw):
try:
# Manually parse JSON data from the request body
data = json.loads(request.httprequest.data.decode('utf-8'))

# Ensure the data is a list
if not isinstance(data, list):
response = {"success": False, "error": "Data should be a list of students."}
return request.make_response(json.dumps(response), headers={'Content-Type': 'application/json'})

study_mode_mapping = {
'Distance': 'distance',
'Fulltime': 'full_time',
'Parttime': 'part_time'
}

nationality_mapping = {
'Local': 'local',
'SADC': 'sadc',
'International': 'international'
}

responses = []
for student_data in data:
for key in student_data:
if isinstance(student_data[key], str):
student_data[key] = urllib.parse.unquote_plus(student_data[key])

_logger.info("Received data: %s", student_data)

study_mode = student_data.get('study_mode')
if study_mode:
student_data['study_mode'] = study_mode_mapping.get(study_mode, 'distance')

nationality = student_data.get('nationality')
if nationality:
student_data['nationality'] = nationality_mapping.get(nationality, 'local')

required_fields = ['student_id', 'government_id', 'address', 'study_mode', 'email', 'name', 'mobile', 'nationality']
missing_fields = [field for field in required_fields if field not in student_data]
if missing_fields:
responses.append({"success": False, "error": f"Missing fields: {', '.join(missing_fields)}"})
continue

taken_reg = request.env['res.partner'].sudo().search([
('government_id', '=', student_data.get('government_id')),
('is_student', '=', True)
], limit=1)

taken_id = request.env['res.partner'].sudo().search([
('student_id', '=', student_data.get('student_id')),
('is_student', '=', True)
], limit=1)

if taken_id:
responses.append({"success": False, "error": "Another Student with this Student ID already exists!"})
continue

if taken_reg:
responses.append({"success": False, "error": "Another Student with this Government ID already exists!"})
continue

student = request.env['res.partner'].sudo().create({
'is_student': True,
'student_id': student_data.get('student_id'),
'government_id': student_data.get('government_id'),
'street': student_data.get('address'),
'study_mode': student_data.get('study_mode'),
'email': student_data.get('email'),
'name': student_data.get('name'),
'mobile': student_data.get('mobile'),
'nationality': student_data.get('nationality'),
})

responses.append({'success': True, 'student_id': student.id})

return request.make_response(json.dumps(responses), headers={'Content-Type': 'application/json'})

except Exception as e:
_logger.error("Error processing request: %s", str(e))
response = {"success": False, "error": str(e)}
return request.make_response(json.dumps(response), headers={'Content-Type': 'application/json'})
Avatar
Descartar
Best Answer

Hi,

If you're getting a 404 error when trying to POST to a custom route in Odoo 17, make sure your controller is defined correctly. Here's a working example:


from odoo import http

from odoo.http import request

import json


class StudentController(http.Controller):


    @http.route(

        '/api/student/create',

        type='http',

        auth='public',

        csrf=False,

        methods=['POST'],

        cors='*'

    )

    def create_student(self, **kw):

        response = {'message': 'success'}

        return request.make_response(json.dumps(response), status=200)



Also, make sure to add a dbfilter in your odoo.conf file. Without it, the request may not map to the correct database, especially if you're running in a multi-database environment.


Hope it helps

Avatar
Descartar
Best Answer

Hii,

Please check 
The file is located in your module’s /controllers/ directory.
The controller file is imported in your module's __init__.py like this:

directory like :
your_module/controllers/__init__.py

from . import controllers


Check Route Matching:

@http.route('/api/student/create', type='http', auth='public', methods=['POST'], csrf=False)

Make sure you're using exact match in Postman:

  • URL should be http://localhost:8069/api/student/create (adjust port as needed).
  • Method must be POST.

Testing Route:

@http.route('/api/test', type='http', auth='public', methods=['GET'], csrf=False)

def test_api(self, **kw):

    return "It works!"

 i hope it is use full


Avatar
Descartar
Related Posts Respostes Vistes Activitat
1
de juny 25
2082
3
de juny 24
3244
1
de març 23
3666
0
d’abr. 17
2879
0
de març 15
3934