简体   繁体   中英

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'
  }
})

The key name should be header s , not header.

fetch(URL, {
  credentials: 'include',
  headers: {
    'Authorization': 'Bearer TOKEN'
  }
})

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM