I’m trying to retrieve the information of a HASS.io sensor on a local webpage using the REST API. Using the same URL and authentication token, a CURL request works but a JS fetch() reports err 401 unauthorized.
The CURL being:
curl -X GET -H "Authorization: Bearer [authToken]" http://[HOSTNAME]/api/states/[entity.id]
And the JS:
const url = "http://[HOSTNAME]/api/states/[entity.id]";
const options = {
mode: 'no-cors',
headers: {
Authorization: "Bearer [authToken]"
}
};
fetch(url, options)
.then( res => res.json() )
.then( data => console.log(data) );
Does anyone know what I’m doing wrong? I don’t have a lot of experience with this.