# Endpoint access

**URL:** <https://discuss.tryton.org/t/endpoint-access/7789>\
**Category:** Developer\
**Created:** [September 3, 2024, 3:33pm UTC](https://discuss.tryton.org/t/endpoint-access/7789 "2024-09-03T15:33:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)\
**Post date:** [September 3, 2024, 3:33pm UTC](https://discuss.tryton.org/t/endpoint-access/7789/1 "2024-09-03T15:33:55Z")

</div>

I made a vuejs face recognition and wanted to send an encode\_image and employee\_id, i already made api in my attendance module:

```auto
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:

```auto
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.

```

i have no idea how to solve this

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [September 4, 2024, 5:48am UTC](https://discuss.tryton.org/t/endpoint-access/7789/2 "2024-09-04T05:48:55Z")

</div>

It seems it is because the `Origin` header is not allowed. It is not the `Host` name and it is not in [`[web] cors`](https://docs.tryton.org/latest/server/topics/configuration.html#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.

---

<div class="post-metadata">

**Author:** ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)\
**Post date:** [September 4, 2024, 9:51am UTC](https://discuss.tryton.org/t/endpoint-access/7789/3 "2024-09-04T09:51:21Z")

</div>

I alread removde the `OPTIONS`

and added cors in trytond.conf

```auto
[web]
listen=127.0.0.1:8002
root=/home/horse/tryton/sao
cors = http://localhost:8080/

```

however this still reacted like this:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [September 4, 2024, 9:54am UTC](https://discuss.tryton.org/t/endpoint-access/7789/4 "2024-09-04T09:54:30Z")

</div>

> [@Siobhan](#):
>
> however this still reacted like this:
> 
> ```auto
> 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
> 
> ```

I do not understand what this means but you should look at the headers of your requests and see what is the `Origin`.

---

<div class="post-metadata">

**Author:** ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)\
**Post date:** [September 4, 2024, 10:02am UTC](https://discuss.tryton.org/t/endpoint-access/7789/5 "2024-09-04T10:02:26Z")

</div>

This is the Header

```auto

Request URL:

http://127.0.0.1:8002/face_recognition/create

Request Method:

OPTIONS

Status Code:

500 INTERNAL SERVER ERROR

Remote Address:

127.0.0.1:8002

Referrer Policy:

strict-origin-when-cross-origin

```

I think I already deleted `OPTIONS` in my `api.py`

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [September 4, 2024, 10:09am UTC](https://discuss.tryton.org/t/endpoint-access/7789/6 "2024-09-04T10:09:15Z")

</div>

Sorry but this does not look like [HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers).

> [@Siobhan](#):
>
> I think I already deleted `OPTIONS` in my `api.py`

I do not understand why you are saying that.

---

<div class="post-metadata">

**Author:** ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)\
**Post date:** [September 4, 2024, 11:03am UTC](https://discuss.tryton.org/t/endpoint-access/7789/7 "2024-09-04T11:03:58Z")

</div>

Sorry, you mean this?

 ![Screenshot from 2024-09-04 18-02-45](https://discuss-cdn.tryton.org/uploads/default/original/2X/1/1ee4045e24937ba6042611f79bff17c4ed98fe61.png)

---

<div class="post-metadata">

**Author:** ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)\
**Post date:** [September 5, 2024, 8:16am UTC](https://discuss.tryton.org/t/endpoint-access/7789/8 "2024-09-05T08:16:03Z")

</div>

or this?

```auto
accept:

application/json, text/plain, */*

accept-encoding:

gzip, deflate, br, zstd

accept-language:

en-US,en;q=0.9

connection:

keep-alive

content-length:

2751

content-type:

application/json

host:

127.0.0.1:8002

origin:

http://localhost:8080

referer:

http://localhost:8080/

sec-ch-ua:

"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"

sec-ch-ua-mobile:

?0

sec-ch-ua-platform:

"Linux"

sec-fetch-dest:

empty

sec-fetch-mode:

cors

sec-fetch-site:

cross-site

user-agent:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

```

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [September 5, 2024, 8:29am UTC](https://discuss.tryton.org/t/endpoint-access/7789/9 "2024-09-05T08:29:30Z")

</div>

We can see that the origin is different from the host, so CORS applies:

> [@Siobhan](#):
>
> ```auto
> host:
> 
> 127.0.0.1:8002
> 
> origin:
> 
> http://localhost:8080
> 
> ```

but in you configuration you put:

> [@Siobhan](#):
>
> `cors = http://localhost:8080/`

But cors should contain allowed origins which does not include the path. So you should use:

```auto
[web]
cors = http:/localhost:8080

```

otherwise with the path it does not match with the actual origin.

---

<div class="post-metadata">

**Author:** ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)\
**Post date:** [September 5, 2024, 6:39pm UTC](https://discuss.tryton.org/t/endpoint-access/7789/10 "2024-09-05T18:39:17Z")

</div>

I already do what you told, I added this:

```auto

[web]
listen=127.0.0.1:8002
root=/home/name/tryton/sao
cors =
       http://localhost:8080

```

But it keep showing this

```auto
accept:
application/json, text/plain, */*

accept-encoding:
gzip, deflate, br, zstd

accept-language:
en-US,en;q=0.9

connection:
keep-alive

content-length:
2742

content-type:
application/json

host:
127.0.0.1:8002

origin:
http://localhost:8080

referer:

http://localhost:8080/

sec-ch-ua:

"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"

sec-ch-ua-mobile:

?0

sec-ch-ua-platform:

"Linux"

sec-fetch-dest:

empty

sec-fetch-mode:

cors

sec-fetch-site:

cross-site

user-agent:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

```

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [September 6, 2024, 9:02am UTC](https://discuss.tryton.org/t/endpoint-access/7789/11 "2024-09-06T09:02:20Z")

</div>

You must debug in `trytond/wsgi.py` why it aborts with `FORBIDDEN`.

---

<div class="post-metadata">

**Author:** ![heisenberg](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@heisenberg](https://discuss.tryton.org/u/heisenberg)\
**Post date:** [February 20, 2025, 8:16am UTC](https://discuss.tryton.org/t/endpoint-access/7789/12 "2025-02-20T08:16:51Z")

</div>

Hi,

Do you find the solution? I’ve experienced the same using Vue.js..
