Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Fetch with Authorization Header and CORS

I'm trying to get my request to go through to a online game API that I can't seem to get working. I'm using the Fetch API, and some request require Authorization Bearer token, but the request never gets sent with the authorization header.

I have tried

mode: 'no-cors',
credentials: 'include'

and obviously putting the Authorization in the header like so

header: { 'Authorization': 'Bearer TOKEN' }

but the request still does not go with the authorization. Can anyone point me in the right direction?

EDIT Heres the way I am making the request

fetch(URL, {
  credentials: 'include',
  header: {
    'Authorization': 'Bearer TOKEN'
  }
})
like image 205
Steven R Avatar asked Feb 05 '23 00:02

Steven R


2 Answers

The key name should be headers, not header.

fetch(URL, {
  credentials: 'include',
  headers: {
    'Authorization': 'Bearer TOKEN'
  }
})
like image 77
Yevgeny Kozlov Avatar answered Feb 06 '23 15:02

Yevgeny Kozlov


Read the documentation

Keys can be passed either via query parameter or HTTP header. Guildwars API servers do not support preflighted CORS requests, so if your application is running in the user's browser you'll need to use the query parameter.

To pass via query parameter, include "?access_token=" in your request.

like image 34
Darkrum Avatar answered Feb 06 '23 14:02

Darkrum