add header to fetch GET method

Issue

This Content is from Stack Overflow. Question asked by Ali Ratel

I have a problem in fetch data ?
in my header , i will send token for authentication to my back-end but i don’t know how i can add header to get method ..

fetch('https://api.github.com/users/mralexgray/repos', {
  method: 'GET',
  header: {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "X-Aequseted-With": "XMLHttpRequest",
    "Authorization": `Bearer ${token}`
  }
}).then((result) => {
  result.json()
  console.log(result)
  if (result.status == 200) {
    async function GetLink() {
      const response = await fetch('https://api.github.com/users/mralexgray/repos')
      const data = await response.json();
      console.log(data)
      sessionStorage.setItem('userAttemps', data[0].id);
      sessionStorage.setItem('freeAttemps', data[0].id);
    }
    GetLink()
  }
}).catch(err => {
  console.error(err);
});
userAttemps = sessionStorage.getItem('userAttemps')
freeAttemps = sessionStorage.getItem('freeAttemps')

Is my code correct?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?