What is axios-cookiejar-support?
The axios-cookiejar-support package is an extension for the popular Axios HTTP client that adds support for handling cookies using a cookie jar. This is particularly useful for maintaining session state across multiple HTTP requests.
What are axios-cookiejar-support's main functionalities?
Setting up a cookie jar
This feature allows you to set up a cookie jar that can be used to store and manage cookies across multiple HTTP requests. The code sample demonstrates how to create a cookie jar using the tough-cookie library and wrap an Axios instance with it.
const axios = require('axios');
const { wrapper } = require('axios-cookiejar-support');
const { CookieJar } = require('tough-cookie');
const jar = new CookieJar();
const client = wrapper(axios.create({ jar }));
Making requests with cookie jar support
Once the Axios instance is wrapped with the cookie jar, you can make HTTP requests as usual, and the cookies will be automatically managed. The code sample shows a simple GET request to an example URL.
client.get('https://example.com')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
Persisting cookies across requests
This feature demonstrates how cookies are persisted across multiple requests. The code sample shows a login request followed by a request to a dashboard, with the cookies from the login request being used in the subsequent request.
client.get('https://example.com/login')
.then(response => {
return client.get('https://example.com/dashboard');
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
Other packages similar to axios-cookiejar-support
tough-cookie
The tough-cookie package is a standalone library for handling cookies in Node.js. It provides a robust implementation of the HTTP cookie standard, including support for cookie jars. While it does not integrate directly with Axios, it can be used in conjunction with other libraries to achieve similar functionality.
request
The request package is a simplified HTTP client for Node.js that includes built-in support for cookies. It is a more comprehensive solution compared to axios-cookiejar-support, as it handles both HTTP requests and cookie management out of the box. However, it is less modular and flexible than using Axios with axios-cookiejar-support.
node-fetch
The node-fetch package is a lightweight module that brings the Fetch API to Node.js. While it does not include built-in cookie support, it can be extended with the tough-cookie library to manage cookies. This combination provides a similar level of functionality to axios-cookiejar-support, but with a different API.
axios-cookiejar-support
Add tough-cookie
support to axios.
Table of Contents
Install
npm install axios tough-cookie axios-cookiejar-support
Usage
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.
Extended Request Config
import type { CookieJar } from 'tough-cookie';
declare module 'axios' {
interface AxiosRequestConfig {
jar?: CookieJar;
}
}
See also https://github.com/axios/axios#request-config .
Contributing
PRs accepted.
License
MIT (c) 3846masa