Skip to content
Advertisement

How do i hide API key when it must be wrapped in apostrophe?

I’m struggling a bit on how to hide the API key that is located in my header.

JavaScript:

headers:{
            'Content-Type': 'application/json',
            'Authorization': 'Token token="API TOKEN HERE"'}
        }

Because the ‘Authorization’ and ‘Token token=’ are wrapped in apostrophe i can’t put in a variable because it would then be seen as a string and the API will deny my request. I tried using string interpolation but didn’t work. The documentation for the API said that the API token needs to be in the header.

The API i use FavQs

Advertisement

Answer

Found out what the problem is, was reading the MDN Fetch API and found this:

headers: {
  'Content-Type': 'application/json'
  // 'Content-Type': 'application/x-www-form-urlencoded',
}

Was so hung up on the apostrophe and put it in my code as well, but the API part didn’t need it.

My code works now:

headers:{
            'Content-Type': 'application/json',
            Authorization: `Token token=${apiKEY}`}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement