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
.
Install
$ npm i axios @3846masa/axios-cookiejar-support
-- OR --
$ npm i axios axios-cookiejar-support
Note | If you don't mind to use a scoped library, please use the scoped library. To put my account name in your dependencies is my pleasure :)
Usage
const axios = require('axios').default;
const axiosCookieJarSupport = require('@3846masa/axios-cookiejar-support').default;
const tough = require('tough-cookie');
axiosCookieJarSupport(axios);
const cookieJar = new tough.CookieJar();
axios.get('https://google.com', {
jar: cookieJar,
withCredentials: true
})
.then(() => {
console.log(cookieJar);
});
See examples.
Extended Request Config
c.f.) https://github.com/mzabriskie/axios#request-config
{
jar: undefined,
withCredentials: false
}
Browser
Running on browser, this library becomes noop (config.jar
might be ignored).
Contribution
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
LICENSE
MIT License
Author
3846masa