Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!