0

I'm working on a project in javascript with the brawl stars api that is on this site "https://developer.brawlstars.com/#/" and I've already followed the documentation, I've checked my ip, my authorization header and I'm always getting the same CORS error: "Access-Control-Allow-Origin header is present on the request resource" follow my code below

const tag = '...'
const token = '...'
const url = 'https://api.brawlstars.com/v1/players/%23'
const options = {
    headers: {
      'Authorization': 'Bearer'+' '+ token
    }
}
fetch(url+tag,options)
.then(res => res.json())
.then(data => console.log(data))
  • If it's really a public API provided by a 3rd party that is causing the CORS error, there isn't anything you can do about it, because CORS headers have to be set at the backend. I don't know that API, but maybe you have to register a domain which is allowed? Or it's not supposed to be used from the browser? – derpirscher Mar 26 '23 at 14:05
  • this api is from a mobile game. I've seen an app on yt with this api, but they don't show code. The only thing the api asks for is my public ip. In the 'Thunder Client' vscode extension gives me the answer – Gabriel Ferreira Mar 26 '23 at 17:17

1 Answers1

0

here are things you need to check:

  1. Check url which getting created in fetch where you have concatenated
  2. Also try to pass headers in the correct way

Thier will be bearer token you need to pass in headers like here i have added my "myheaders" which will be token with key,

var requestOptions = {
  method: 'GET',
  headers: myHeaders,
  redirect: 'follow',

};

fetch("https://api.brawlstars.com/v1/players/%23", requestOptions)
  .then(response => response.json())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
  1. Try to pass
method: 'GET',
      headers: {
        accept: 'application/json',
      },
  • I tried your suggestion but it keeps giving me the same error. I tested it in the 'Thunder Client' vscode extension with my token and the response came out fine, but when I use it in my script it follows the error – Gabriel Ferreira Mar 26 '23 at 16:48