0

Tried the API example listed on Home Assistant, it worked with the Token and I can sucsessfully get the header. When trying to use fetch on js I can only get 401 (Unauthorized). Im trying to access the RestAPI of Home Assistant via JavaScript on Chrome. My Example:

fetch('http://IP_ADRESS_XY:8123/api/', 
        {
        mode: 'no-cors',    
        headers: {
                "Authorization": "Bearer TOKEN_XY",
                "Content-Type": "application/json",}
        })   
         .then(response => response.text())
         .then(text => console.log(text))

When running this example with python im not getting Error 401 (Unauthorized):

from requests import get

url = "http://IP_ADRESS_XY:8123/api/"
headers = {
    "Authorization": "Bearer TOKEN_XY",
    "Content-Type": "application/json",
}

response = get(url, headers=headers)
print(response.text)

Token and IP replaced for privacy in stack overflow

I tried multible modes and changing the token a few times to a new one. How can i get the code runing in javascript.

  • `mode: 'no-cors'` lets you only send _"no-CORS-safelisted request-header names"_ - and `Authorization` is not one of them. – CBroe Feb 13 '23 at 12:57

0 Answers0