add header to fetch GET method


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('', {
  method: 'GET',
  header: {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "X-Aequseted-With": "XMLHttpRequest",
    "Authorization": `Bearer ${token}`
}).then((result) => {
  if (result.status == 200) {
    async function GetLink() {
      const response = await fetch('')
      const data = await response.json();
      sessionStorage.setItem('userAttemps', data[0].id);
      sessionStorage.setItem('freeAttemps', data[0].id);
}).catch(err => {
userAttemps = sessionStorage.getItem('userAttemps')
freeAttemps = sessionStorage.getItem('freeAttemps')

Is my code correct?


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?