CORS error when making an Axios call

Avatar image for ermekbarmashev
ErmekBarmashev

3

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

I'm trying to get a response using axios but keep getting a cors error. It perfectly works in postman. Maybe someone can help? The code is below:

axios({ url: "https://www.giantbomb.com/api/company/3010-82/?api_key=[API_KEY]&format=json", method: "get", })

.then((res) => { console.log("works"); })

.catch(() => { console.log("doesnt work"); });

Avatar image for peezmachine
PeezMachine

562

Forum Posts

32

Wiki Points

0

Followers

Reviews: 4

User Lists: 1

#2  Edited By PeezMachine

Are query args supposed to be passed in directly via the url, or should they be passed in via a "params" argument?

EDIT: Nvm just got to the part where they are equivalent.

Avatar image for dancingpolkabear
dancingpolkabear

235

Forum Posts

44

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#3  Edited By dancingpolkabear

Hi Ermek, might be worth reviewing cors first https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS. If you are making a request on the frontend from your site to an external domain, it needs to pass a cors test in modern browsers which isn't generally typical for an api like this to wildcard.

While I haven't worked with the gb api, in general it's not great practice to work with an external api in the frontend of your app. That would expose your api key and enable other users to make malicious requests on your behalf.

Instead have you looked into doing the necessary requests on your server and giving the response to the client? You won't need to bother with cors and can keep your secrets safe.

Cheers!

Avatar image for ermekbarmashev
ErmekBarmashev

3

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

@dancingpolkabear: Thank you for your answer! I'm just making a pet project for myself so security was not one of my concerns. I'm not experienced in backend so this is a good opportunity to try it. Cheers!