Hello Tobias, awesome add-on you made for the community. I have been using it for 2+ months now and it comes in handy when I am away from home.
Currently, I am trying to create a small web app and it would require the HA REST API through the domain I use for this add-on. When I test the API (https://xxxxxxxxx.xxx/api
) on Postman with the bearer token, I get a successful message and even data when I use the /api/states
.
However, when I try to load the web app on my local host, I keep getting CORS errors.
Sample of my code
const url = "https://xxxxxxxxx.xxx/api/states"; // API URL
const token = ""; // API Token
const method = "GET"; // Request method, change for what's needed
fetch(url, {
method,
mode: 'cors',
headers: {
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': '*',
'Content-Type': 'application/json',
"Authorization": `Bearer ${token}` // This is the important part, the auth header
}
}).then(res => console.log(res)).then(data => console.log(data)).catch(err => console.error(err)); // Do better handling here
Do you have an idea of what I should do? Any suggestion? Thanks