Add tough-cookie
support to axios.
npm install axios tough-cookie axios-cookiejar-support
import axios from 'axios';
import { wrapper } from 'axios-cookiejar-support';
import { CookieJar } from 'tough-cookie';
const jar = new CookieJar();
const client = wrapper(axios.create({ jar }));
await client.get('https://example.com');
See examples for more details.
import type { CookieJar } from 'tough-cookie';
declare module 'axios' {
interface AxiosRequestConfig {
jar?: CookieJar;
}
}
See also https://github.com/axios/axios#request-config .
- Q. Why can't I assign the httpAgent / httpsAgent?
- A. axios-cookiejar-support uses httpAgent / httpsAgent to read and write cookies. If other Agents are assigned, cookies cannot be read/written.
- Q. I want to use it with another Agent (e.g., http-proxy-agent).
- A. Consider using http-cookie-agent. axios-cookiejar-support also uses http-cookie-agent. Read http-cookie-agent's README for more details.
PRs accepted.