I am encountering a CORS (Cross-Origin Resource Sharing) error when making a POST request to an API endpoint in Odoo from a web page. The error occurs when the browser attempts to send data to the endpoint, while the request works fine using tools like Postman. The browser’s console shows a CORS error indicating that the request is blocked due to missing or incorrect CORS headers. Despite including headers like Content-Type: application/json the issue persists. The Odoo server logs indicate that an OPTIONS request is sent before the POST request, which is typical for preflight requests to check CORS permissions. However, the server-side configuration may not be set up to handle these OPTIONS requests correctly.
This is my route
@http.route('/api/create_employee', type='json', auth='none', methods=['POST'], csrf=False, cors='*')
This is my header
response = request.make_response(
json.dumps({'message': 'Employee created successfully'}),
headers={
'Access-Control-Allow-Origin': '*', # Adjust to restrict access as needed
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Authorization, talview-odoo-api',
'Access-Control-Allow-Credentials': 'true',
"Content-Type": "application/json"
}
)
return response
Thank you in advance for your help!
Hi, did you find a solution?
Looks like Odoo doesn't care about us. They have their own implementation for the JSON RPC and they're using that for enterprise. There have been CORS errors reported on this forum for over a year without any solution from the company or the community.
Add an executable example of your source and I'm sure a solution can be found. JSON RPC is certainly NOT limited to Enterprise setups. Also, did you try to actually work your way through https://www.odoo.com/documentation/18.0/developer/reference/external_api.html?
Hi, there was nothing I could do at Odoo's end. I tried every possible way but it still was not working.
The only solution that I found was that to setup a reverse proxy at the client's end so that it sends a request to the client's domain instead of odoo's domain directly.
Not sure if there was any solution apart from setting up reverse proxy at client's end. Hopefully, someone has a solution for this issue