0

I am trying to understand why the chrome (and firefox) devtools network tab can see request/response cookies in an initial GET of a website, but node-fetch cannot.

For example, take stackoverflow.com. When I copy the initial GET with Google Chrome as a node fetch, enter image description here

and try to print the request cookies, I don't get any. This is strange because per the image above, request cookies are provided by stack overflow in response to this GET.

let fetch = require('node-fetch')

fetch("https://stackoverflow.com/", {
  "headers": {
    "accept": "text/html,application/xhtml+xml,application/xml;..."
    "accept-language": "en-US,en;q=0.9",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
    "sec-ch-ua-mobile": "?0",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "none",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "cookie": "OptanonAlertBoxClosed=2021-04-07T22:42:43.384Z;..."
  },
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": null,
  "method": "GET",
  "mode": "cors"
}).then(async (resp)=> {
    console.log(resp.headers.get('set-cookie'))
});

(the console logs null). I've deleted most of the cookies in this request for brevity, but you can see the OptanonAlertBoxClosed in the copied fetch and in the request cookies in the image above.

Question

Why does Chrome devtools log request cookies for a GET to stackoverflow.com, but node-fetch does not?

Ross Jacobs
  • 2,962
  • 1
  • 17
  • 27
  • Where do the request cookies come from? – Wais Kamal Jun 15 '21 at 19:33
  • My understanding is that these are provided in response to my initial GET of stackoverflow.com. I did not generate the ones you see in the devtools console. – Ross Jacobs Jun 15 '21 at 19:36
  • 1
    Probably the ones you see there are the ones previously stored in your browser. What I meant is that when you fetch something in node you get to choose what cookies to send! – Wais Kamal Jun 15 '21 at 19:39
  • I need to do some more investigation, but this is likely the source of my confusion - thank you! – Ross Jacobs Jun 15 '21 at 19:42

0 Answers0