
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
axios-cookiejar-support
Advanced tools
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.
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);
});
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.
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.
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.
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 .
PRs accepted.
FAQs
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.