I made a vuejs face recognition and wanted to send an encode_image and employee_id, i already made api in my attendance module:
from trytond.wsgi import app
from trytond.pool import Pool
from werkzeug.wrappers import Response
from flask import request, make_response
@app.route('/face_recognition/create', methods=['POST', 'OPTIONS'])
def create_face_record():
if request.method == 'OPTIONS':
response = make_response('')
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
return response
data = request.json
employee_id = data.get('employee_id')
face_encoding = data.get('face_encoding')
if not employee_id or not face_encoding:
return {'error': 'Missing employee_id or face_encoding'}
try:
FaceRecognition = Pool().get('face.recognition')
record = FaceRecognition.create_face_record_api(employee_id, face_encoding)
response = {'success': True, 'record_id': record.id}
except ValueError as e:
response = {'error': str(e)}
# Header CORS
resp = make_response(response)
resp.headers['Access-Control-Allow-Origin'] = '*'
resp.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
resp.headers['Access-Control-Allow-Headers'] = 'Content-Type'
return resp
but i got this denied access error:
Traceback (most recent call last):
File "/home/name/.virtualenvs/project/lib/python3.10/site-packages/werkzeug/serving.py", line 370, in run_wsgi
execute(self.server.app)
File "/home/name/.virtualenvs/project/lib/python3.10/site-packages/werkzeug/serving.py", line 331, in execute
application_iter = app(environ, start_response)
File "/home/name/tryton/trytond/trytond/wsgi.py", line 204, in __call__
return self.wsgi_app(environ, start_response)
File "/home/name/tryton/trytond/trytond/wsgi.py", line 210, in __call__
return self.app(environ, start_response)
File "/home/name/tryton/trytond/trytond/wsgi.py", line 176, in wsgi_app
abort(HTTPStatus.FORBIDDEN)
File "/home/name/.virtualenvs/project/lib/python3.10/site-packages/werkzeug/exceptions.py", line 878, in abort
_aborter(status, *args, **kwargs)
File "/home/name/.virtualenvs/project/lib/python3.10/site-packages/werkzeug/exceptions.py", line 863, in __call__
raise self.mapping[code](*args, **kwargs)
werkzeug.exceptions.Forbidden: 403 Forbidden: You don't have the permission to access the requested resource. It is either read-protected or not readable by the server.
It seems it is because the Origin header is not allowed. It is not the Host name and it is not in [web] cors. Or maybe the Origin header is null so in this case you must decorate the entrypoint function with @allow_null_origin from trytond.protocols.wrapper.
By the way you do not need to add support for OPTIONS in the entrypoint, trytond as a default catch all route that support them.
[web]
listen=127.0.0.1:8002
root=/home/horse/tryton/sao
cors = http://localhost:8080/
however this still reacted like this:
Access to XMLHttpRequest at 'http://127.0.0.1:8002/face_recognition/create' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
index.js??clonedRule…e=script&lang=js:79 Error mengirim data ke Trytond:
1. AxiosError {message: 'Network Error', name: 'AxiosError', code: 'ERR_NETWORK', config: {…}, request: XMLHttpRequest, …}
xhr.js:186 POST http://127.0.0.1:8002/face_recognition/create net::ERR_FAILED
index.js??clonedRule…e=script&lang=js:58 Employee ID: 1