axios-cookiejar-support
axios-cookiejar-support copied to clipboard
Retrying the same request doesn't work: does not support for use with other http(s).Agent
Describe the bug
In case of a retry (e.g. via axios-retry) the following error is thrown:
https://github.com/3846masa/axios-cookiejar-support/blob/7b85d03ce3134915fb3a08077a6bf3973ea67ef1/src/index.ts#L21-L23
Reason: the same interceptor has already set proper agents to the request config in a previous failed request.
The retryCount on the screenshot is 1 which means that the same requested has already failed once and this is the first retry.
To Reproduce
Steps to reproduce the behavior:
run the code below.
Expected behavior
No error.
Code that reproduces the bug
const axios = require("axios");
const { CookieJar } = require('tough-cookie');
const client = axios.create({jar: new CookieJar()});
const axiosCookieJarSupport = require('axios-cookiejar-support').wrapper;
axiosCookieJarSupport(client);
const retry = require("axios-retry");
retry(client);
(async function () {
await client("http://httpstat.us/503");
})()
Environments
- OS: macOS
- Node.js version: 14.20.0
- axios version: 1.1.2
- tough-cookie version: 4.1.2
- axios-cookiejar-support: 4.0.3
Maybe you can simply check if an http(s) agent is a CookieAgent?
Also, FYI axios-retry removes http(s) agents if they are default ones, but I am not sure if other libs do the same (e.g. retry-axios):
https://github.com/softonic/axios-retry/blob/9adaa6fe38175e696520fd6ba1b95231b89990f8/es/index.mjs#L113-L118
Also, a question: what to do in case of a proxy with authentication? We have a helper function that creates a pre-configured http agent to handle proxy with auth, but we cannot use it.
@grenik Hello, funny I ran into the same exact issue. I decided to try my hand at a PR to implement a fix for this to allow agents if they are instances of Http(s)CookieAgent. Check it out, let me know if the approach is okay #609
Same for me:
import * as axios from 'axios';
import {AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosStatic, Method} from 'axios';
import {CookieJar, MemoryCookieStore} from 'tough-cookie';
import {wrapper} from 'axios-cookiejar-support';
.
.
this.options = _.defaults(options, {
jar: new CookieJar(new MemoryCookieStore(), {looseMode: true}),
headers: {}
});
.
.
const axiosInstance: AxiosInstance = ((axios as unknown) as AxiosStatic).create({
httpsAgent: new HttpsCookieAgent({
cookies: {jar: this.options.jar},
rejectUnauthorized: false,
keepAlive: true,
timeout: 100000
})
});
this.client = wrapper(axiosInstance);
leads to:
Error: axios-cookiejar-support does not support for use with other http(s).Agent.
used version:
"axios-cookiejar-support": "^4.0.3",
Please add either the PR from Nikolay or Andrew and release a new version.
Sorry for the late reply.
axios-cookiejar-support is a wrapper for http-cookie-agent. https://github.com/3846masa/http-cookie-agent
axios-cookiejar-support does not support the using with other Agents or using proxies.
For advanced use cases, use http-cookie-agent.
Please read the README for http-cookie-agent for more details.
The problem is, that we indeed are trying to use the requested http(s)-cookie-agent. Even this is not working as described in the code above.
The problem is the check here:
https://github.com/3846masa/axios-cookiejar-support/blob/main/src/index.ts#L21
if (config.httpAgent || config.httpsAgent) {
throw new Error('axios-cookiejar-support does not support for use with other http(s).Agent.');
}
config.httpAgent = new HttpCookieAgent({ cookies: { jar: config.jar } });
config.httpsAgent = new HttpsCookieAgent({ cookies: { jar: config.jar } });
It prevents ANY injection of a customized http-agent even if it is of type http-cookie-agent.
The provided pull request from andrewkucz https://github.com/3846masa/axios-cookiejar-support/pull/609 is checking this and provides a solution.
Please check it
axios-cookiejar-support is just a wrapper for http-cookie-agent.
When using http-cookie-agent directly, there is no need to use axios-cookiejar-support.
I have also run into this issue. We aren't using http-cookie-agent directly or trying to use a different agent, rather the original agent created by this library is specfied in a replay of a request.
The problem is when, for example, libraries replay the request (e.g. after a 429 response code) they resupply the config but since this library has added http[s]Agent to the config a second request will always fail because this library now checks for the presence of that http[s]Agent property (which is always there on a replay because this library added it to the config on the first request).
So the check in this library simply for the presence of the http[s]Agent property in config breaks those kinds of replays. The proposal in #609 to allow http[s]Agent property values that are of type Http[s]CookieAgent can solve the problem because if the original agent is resupplied the request is allowed to happen. Clearly that check would also allow any other configured Http[s]CookieAgent instance, which I understand feels like allowing mis-use of this library. I can't think of one, but perhaps there is a tighter check that is still lenient enough to allow replays.
Thanks @ricellis. I understand the issue now.
I had not expected a use case where config is reused.
We will make a fix soon to allow config to be reused.