Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
Headers in react don't persist

Okay so this may be a really junior question but I have just started working with react and I don't know much about it.

The thing is: I'm trying to code an ecommerce. To access a certain page of the website (productos), you need a token (to be logged in, authenticated). So the first thing I do in each page (that requires to be logged in) is to check if the user is logged in, and if it is, I take the token and I set it to the header like this:

function Products()
{
Promise.all([
    fetch('http://localhost:3001/login',
    {
      method: 'get',
      headers: new Headers({
        'Authorization': sessionStorage.getItem("token"),
        'Content-Type': 'application/x-www-form-urlencoded'
      })
    }).then(value => value.json()), 
    fetch('http://localhost:3001/productos').then(value => value.json())
  ]).then((value) => {
    console.log(value)
  })
}

The first fetch is working fine, because it sets the header 'authorization' equals to the token of the user, BUT the second fetch doesn't have this header.

enter image description here

enter image description here

I thought that the header would persist... Since it doesn't, when the fetch of products tries to load the data it doesn't do it, because it doesn't have the header 'authorization'...

Could somebody help me out please?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda