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

147
Vistas
Why do I have to refresh my Express endpoint when trying to grab req.headers.cookies from frontend

I am running into a problem where I have to refresh my express endpoint to be able to read req.headers.cookie If I do not refresh the endpoint then console.log(req.headers.cookie) returns undefined instead of the assigned value. I have included some photos below to better show you what my problem is I have also included my back-end code that sets the cookie and should also console.log(req.headers.cookie) Thanks in advance for any help!

Images:

First time I hit the Login button this is the cookie I get. First Login Attempt

First Response

Seconed time I hit the login button this is the cookie I get. Seconded Login Attempt

Notice below on our second back-end response how the AccessToken value is equal to the first response we received on our front-end. Almost as if our backend code is always reading the previous value of req.headers.cookie instead of the current value. Seconded Response

Back-End Code:

app.get('/', (req, res) => {

try {
    res.cookie("AccessToken", globalToken, {
        sameSite: 'strict',
        maxAge: ((((1000 * 60) * 60) * 24) * 7), /* expire a week from today */
        httpOnly: true,
        path: '/'
        // secure: true
    });

    res.send();

} catch (error) {
    console.log(error)
} finally {
    console.log(req.headers.cookie);
    frontendCookie = req.headers.cookie;
}

});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

HTTP works by having a client (like a browser) make a request. The server then sends a response.

If the response tells the client to store cookies, then they will be sent back to the server on subsequent requests.

So the order of events is:

  1. Browser makes request with no cookies
  2. You send response to set cookies
  3. You look for cookies on the request and don't find any
  4. Browser makes a second request with the cookies from the first response
  5. Server can now read the cookies from the second request

You seem to be expecting the first request to be modified with the cookies that won't exist until the first response is processed.


If you want to take data from either cookies or elsewhere, then you need logic more along the liens of:

let access_token = req.headers.cookie;
if (some condition to set a new access token) {
    access_token = get_new_access_token_value();
    res.cookie( arts to set to access_token );
}
do_something_with(access_token);
about 4 years ago · Juan Pablo Isaza Denunciar
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