Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda