#StackBounty: #node.js #cookies #axios Perserver cookies between request AXIOS (Node.js)

Bounty: 50

$ node -v v10.15.0
"axios": "^0.19.2",

I am trying to keep cookies from response header field ‘set-cookie’ – just like browsers do.
I used to use this module (https://www.npmjs.com/package/request ) and there was an option request.defaults({jar: true}) which worked well.

For axios I reqd that {withCredentials: true} would do the job – but it DOESN’T.

Here is an example code:

axios({ url: 'https://google.com/', method: 'get', withCredentials: true })
  .then((res) => {
    console.log('res.headers = ', res.headers);
  })
  .catch((err) => {
    console.log('ERROR >>>> ', err);
  });

axios({ url: 'https://google.com/', method: 'get', withCredentials: true })
  .then((res) => {
    // console.log("res.headers = ", res.headers);
    console.log('REQUEST HEADERS: ', res.request._header);
  })
  .catch((err) => {
    console.log('ERROR >>>> ', err);
  });

The console result is the following:

res.headers = {
  date: 'Tue, 08 Sep 2020 09:42:24 GMT',
  expires: '-1',
  'cache-control': 'private, max-age=0',
  'content-type': 'text/html; charset=ISO-8859-1',
  p3p: 'CP="This is not a P3P policy! See g.co/p3phelp for more info."',
  server: 'gws',
  'x-xss-protection': '0',
  'x-frame-options': 'SAMEORIGIN',
  'set-cookie': [
    '1P_JAR=2020-09-08-09; expires=Thu, 08-Oct-2020 09:42:24 GMT; path=/; domain=.google.com; Secure',
    'NID=204=hqIOtyoskrispJi7WvceHmix8PF_tVmV6FArMX_LwcvaFRNYzTj85TZp-MXsmOXL0STJBUX0LjsUXpuPF__yx7urLiXyxFKiFsi8RpPheqyrP4XNTwrHXqXPQVqxY0KEpO_XS9ITMVrMGJ5GBSfQB4Ii63o6x4icknqZ-1k4FA8; expires=Wed, 10-Mar-2021 09:42:24 GMT; path=/; domain=.google.com; HttpOnly',
  ],
  'alt-svc':
    'h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
  'accept-ranges': 'none',
  vary: 'Accept-Encoding',
  connection: 'close',
  'transfer-encoding': 'chunked',
};

REQUEST HEADERS: GET / HTTP/1.1 Accept: application/json, text/plain, */* User-Agent: axios/0.19.2 Host: www.google.com Connection: close

As you can see the first response provides many cookies to be set, but the consecutive request doesn’t send them.

How to make the axios keep the cookies between requests?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.