3846masa / axios-cookiejar-support

Add tough-cookie support to axios.
MIT License
315 stars 54 forks source link

Error: axios-cookiejar-support does not support for use with other http(s).Agent. #426

Closed schlomo closed 2 years ago

schlomo commented 2 years ago

How can I use both cookies and HTTPS Agent configuration?

I use Axios like this and I need to disable CA checks:

const
    axios = require('axios').default,
    { wrapper } = require('axios-cookiejar-support'),
    { CookieJar } = require('tough-cookie');

wrapper(axios);
axios.defaults.jar = new CookieJar();
axios.defaults.withCredentials = true;
axios.defaults.httpsAgent = new https.Agent({
    keepAlive: true,
    rejectUnauthorized: false, // disable CA checks
});
axios.defaults.headers.common = {
    "User-Agent": "HTTPie/2.3.0",
    "Accept": "*/*"
};

I get the not supported error. Can you please suggest how to use axios-cookiejar-support in this scenario?

Context: https://github.com/schlomo/chromebooks-in-deutschland/blob/master/functions/backend.js#L16 and I am trying to upgrade my dependencies

Environments

Thanks a lot!

3846masa commented 2 years ago

@schlomo

Thank you for using the library.

axios-cookiejar-support uses http-cookie-agent, an extension of http(s).Agent, to manipulate cookies.

Therefore, axios-cookiejar-support cannot be used with other http(s).Agent.

If you want to pass options to http(s).Agent, you can use http-cookie-agent directly.

const
  axios = require('axios').default,
  { CookieJar } = require('tough-cookie'),
  { HttpCookieAgent, HttpsCookieAgent } = require('http-cookie-agent');

const jar = new CookieJar();

axios.defaults.httpAgent = new HttpCookieAgent({
  jar,
  keepAlive: true,
  rejectUnauthorized: false, // disable CA checks
});
axios.defaults.httpsAgent = new HttpsCookieAgent({
  jar,
  keepAlive: true,
  rejectUnauthorized: false, // disable CA checks
});
axios.defaults.headers.common = {
  'User-Agent': 'HTTPie/2.3.0',
  Accept: '*/*',
};
schlomo commented 2 years ago

Ah, thanks a lot! I didn't realize that. Could you maybe add this to the README?

samislam commented 2 months ago

I want to persist cookies across requests, I am on the server, and I'm using Axios & tough cookie and I am also using axios-cookiejar-support. I want to set the property rejectUnauthorized: false to skip an SSL certification error I am having in my code.

But the problem is that axios-cookiejar-support does not allow to use of a different agent. For me to set this option (rejectUnauthorized), I need to use a new agent (that's what I know).

so what's the solution?

@schlomo

Thank you for using the library.

axios-cookiejar-support uses http-cookie-agent, an extension of http(s).Agent, to manipulate cookies.

Therefore, axios-cookiejar-support cannot be used with other http(s).Agent.

If you want to pass options to http(s).Agent, you can use http-cookie-agent directly.

const
  axios = require('axios').default,
  { CookieJar } = require('tough-cookie'),
  { HttpCookieAgent, HttpsCookieAgent } = require('http-cookie-agent');

const jar = new CookieJar();

axios.defaults.httpAgent = new HttpCookieAgent({
  jar,
  keepAlive: true,
  rejectUnauthorized: false, // disable CA checks
});
axios.defaults.httpsAgent = new HttpsCookieAgent({
  jar,
  keepAlive: true,
  rejectUnauthorized: false, // disable CA checks
});
axios.defaults.headers.common = {
  'User-Agent': 'HTTPie/2.3.0',
  Accept: '*/*',
};

I tried this solution, it solves the problem of the SSL certificate, but I can't use the wrapper function anymore because once I do, it throws an error

Error: axios-cookiejar-support does not support for use with other http(s).Agent.

And because I am not using the wrapper function, the jar is not being remembering the cookies