I need quick help for making a request do the Mattermost API of a self hosted server.
I try to do the following inside javascript:
fetch("https://mydomain/api/v4/users/someuserid", {
method: "GET",
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + 'my personal acess token', 'X-Requested-With': 'XMLHttpRequest'}
}).then((response) => response.json())
.then((data) => {console.log(data)})
I’m testing this on my local machine by opening the javascript console of Firefox. If i have opened a tab with “https://mydomain” the request works perferctly. However, i need to execute this later on a different website with a different domain. So when i try to open google.com and do the same i get a CORS error.
XHROPTIONS https://mydomain/api/v4/users/someuserid CORS Missing Allow Origin
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://mydomain/api/v4/users/someuserid. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 404.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://mydomain/api/v4/users/someuserid. (Reason: CORS request did not succeed). Status code: (null).
Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.
I have tried to set the “Enable cross-origin requests from:” option to * inside the system console but it had no effect.
The relevant part of the nginx config looks like this at the moment:
server {
listen 80;
server_name my-domain;
location / {
client_max_body_size 50M;
proxy_set_header Connection "";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Frame-Options SAMEORIGIN;
proxy_buffers 256 16k;
proxy_buffer_size 16k;
proxy_read_timeout 600s;
proxy_cache mattermost_cache;
proxy_cache_revalidate on;
proxy_cache_min_uses 2;
proxy_cache_use_stale timeout;
proxy_cache_lock on;
proxy_http_version 1.1;
proxy_pass http://backend;
}
}
Adding ‘Access-Control-Allow-Origin’: ‘*’ to the header in the javascript code does also not help.
Can someone please help me out with this?