Hairi
Hairi

Reputation: 3715

Preserve cookies between requests using AXIOS (Node.js)

$ 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?

Upvotes: 5

Views: 7572

Answers (1)

Nicholas Carey
Nicholas Carey

Reputation: 74227

Well... I'm too lazy to do things the hard way, so I'd just do something like this, using axios-cookie-jar-support:

const axios = require('axios').default;
const axiosCookieJarSupport = require('axios-cookiejar-support').default;
const tough = require('tough-cookie');
 
axiosCookieJarSupport(axios);
 
const cookieJar = new tough.CookieJar();
 
axios
  .get('https://google.com', {
    jar: cookieJar, // tough.CookieJar or boolean
    withCredentials: true, // If true, send cookie stored in jar
  })
  .then(() => {
    console.log(cookieJar);
  });

Upvotes: 7

Related Questions