I try to fetch some data from an API, I need to authenticate myself with a Bearer Token. When I test my calls to the API with Insomnia or https://reqbin.com/ It works without any problems, when I try to implement the exact same code in my frontend call it will not work.
My Frontend Call:
const options = {
method: 'GET',
mode: 'no-cors',
headers: {
"Access-Control-Allow-Origin" : "*",
"Authorization": `Bearer ${localStorage.getItem('token')}`
}
};
fetch('http://somedomain/api/auth/Info', options)
.then(response => console.log(response.json()))
.then(response => console.log(response))
.catch(err => console.error(err));
I get the following errors:
GET http://somedomain/api/auth/Info net::ERR_ABORTED 400 (Bad Request)
I do use svelte so I also get as the error message the following: +page.svelte:21 Uncaught (in promise) SyntaxError: Unexpected end of input (at +page.svelte:21:56) at +page.svelte:21:56
.then(response => console.log(response.json()))
.then(response => console.log(response))
.catch(err => console.error(err));
I also tried it with cors, the error messages are the following:
Access to fetch at 'http://somedomain/api/auth/Info' from origin 'http://127.0.0.1:5173' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
GET http://somedomain/api/auth/Info net::ERR_FAILED
Hopefully, someone has an idea of how to fix it. Thanks in advance