2
votes

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

1
Can you try it with moving the second axios request to the .then part of the first one? I wonder if the second is getting executed before first one's response is received...Mavi Domates
Ping @Hairi - have you tried it?Mavi Domates
stackoverflow.com/questions/43002444/… might suit your purpose. or the axios with cookie jar npm packageApocalypseCalculator

1 Answers

3
votes

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);
  });