Competing requests using Xios without losing the session

Asked

Viewed 450 times

1

I am developing a Crawler using Xios. How can I make multiple requests or callbacks without losing the session and without having to manipulate cookies? Note the example below. When logging in, I receive a set-cookie in the reply header, but I expect Xios itself to configure it.

function login() {
  return axios.post('/login');
}

function getUser() {
  return axios.get('/user/12345/');
}

axios.all([login(), getUser()])
  .then(axios.spread(function (acct, perms) {

  }));

I’ve tried with withCredentials but no results

  • Describing the general problem you will get only a general answer. Example: "How to build a house?" , "Use blocks and cement, build solid walls". Instead ask something specific and responsive in a useful way: "How to lift a wall using this type of block with such a slope and such height safely?" , answer: "Position the blocks in such format, run this block placement algorithm, do not use this tool because there is such a risk, here is an example running from a wall ready for you to see how it does [link]". See? Too many wide questions don’t help.

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

1 answer

1

You can use a module that supports cookies in his axios, such as the axios-cookiejar-support.

axios-cookiejar-support

const axios = require('axios').default;
const axiosCookieJarSupport = require('@3846masa/axios-cookiejar-support').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);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.