Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

http-cookie-agent

3846masa1.1mMIT6.0.8TypeScript support: included

Allows cookies with every Node.js HTTP clients.

agent, axios, cookies, fetch, got, http, https, needle, node-fetch, phin, request, superagent, tough-cookie, urllib, undici

readme

HTTP Cookie Agent

HTTP Cookie Agent

github sponsors npm license standard-readme compliant

Allows cookies with every Node.js HTTP clients (e.g. Node.js global fetch, undici, axios, node-fetch).

Table of Contents

Install

npm install http-cookie-agent tough-cookie

When you want to use Node.js global fetch (aka. undici), you should install undici additionally.

npm install undici

Usage

See also examples for more details.

Supported libraries

Library Supported?
Node.js global fetch
undici
node:http
node:https
axios
node-fetch
got *1
superagent *1
request *1
needle
phin
@hapi/wrech
urllib
Bun global fetch *2
Deno global fetch *2

*1: This library supports cookies by default. You may not need http-cookie-agent.

*2: There have proprietary fetch implementation and is not currently supported.

Node.js global fetch

http-cookie-agent supports global fetch since Node.js v18.2.0.

import { CookieJar } from 'tough-cookie';
import { CookieAgent } from 'http-cookie-agent/undici';

const jar = new CookieJar();
const agent = new CookieAgent({ cookies: { jar } });

await fetch('https://example.com', { dispatcher: agent });

undici

import { fetch } from 'undici';
import { CookieJar } from 'tough-cookie';
import { CookieAgent } from 'http-cookie-agent/undici';

const jar = new CookieJar();
const agent = new CookieAgent({ cookies: { jar } });

await fetch('https://example.com', { dispatcher: agent });

node:http / node:https

import https from 'node:https';

import { CookieJar } from 'tough-cookie';
import { HttpsCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();
const agent = new HttpsCookieAgent({ cookies: { jar } });

https.get('https://example.com', { agent }, (res) => {
  // ...
});

axios

import axios from 'axios';
import { CookieJar } from 'tough-cookie';
import { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();

const client = axios.create({
  httpAgent: new HttpCookieAgent({ cookies: { jar } }),
  httpsAgent: new HttpsCookieAgent({ cookies: { jar } }),
});

await client.get('https://example.com');

node-fetch

import fetch from 'node-fetch';
import { CookieJar } from 'tough-cookie';
import { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();

const httpAgent = new HttpCookieAgent({ cookies: { jar } });
const httpsAgent = new HttpsCookieAgent({ cookies: { jar } });

await fetch('https://example.com', {
  agent: ({ protocol }) => {
    return protocol === 'https:' ? httpsAgent : httpAgent;
  },
});

got

:warning: got supports cookies by default. You may not need http-cookie-agent.

See https://github.com/sindresorhus/got/tree/v11.8.2#cookies.

import got from 'got';
import { CookieJar } from 'tough-cookie';
import { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();

const client = got.extend({
  agent: {
    http: new HttpCookieAgent({ cookies: { jar } }),
    https: new HttpsCookieAgent({ cookies: { jar } }),
  },
});

await client('https://example.com');

superagent

:warning: superagent supports cookies by default. You may not need http-cookie-agent.

See https://github.com/visionmedia/superagent/blob/v6.1.0/docs/index.md#saving-cookies.

import superagent from 'superagent';
import { CookieJar } from 'tough-cookie';
import { MixedCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();
const mixedAgent = new MixedCookieAgent({ cookies: { jar } });

const client = superagent.agent().use((req) => req.agent(mixedAgent));

await client.get('https://example.com');

request

:warning: request supports cookies by default. You may not need http-cookie-agent.

See https://github.com/request/request/tree/v2.88.1#examples.

import request from 'request';
import { CookieJar } from 'tough-cookie';
import { MixedCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();

const client = request.defaults({
  agent: new MixedCookieAgent({ cookies: { jar } }),
});

client.get('https://example.com', (_err, _res) => {
  // ...
});

needle

import needle from 'needle';
import { CookieJar } from 'tough-cookie';
import { MixedCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();

await needle('get', 'https://example.com', {
  agent: new MixedCookieAgent({ cookies: { jar } }),
});

phin

import phin from 'phin';
import { CookieJar } from 'tough-cookie';
import { MixedCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();

await phin({
  url: 'https://example.com',
  core: {
    agent: new MixedCookieAgent({ cookies: { jar } }),
  },
});

@hapi/wreck

import Wreck from '@hapi/wreck';
import { CookieJar } from 'tough-cookie';
import { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';

const jar = new CookieJar();

const client = Wreck.defaults({
  agents: {
    http: new HttpCookieAgent({ cookies: { jar } }),
    https: new HttpsCookieAgent({ cookies: { jar } }),
    httpsAllowUnauthorized: new HttpsCookieAgent({ cookies: { jar } }),
  },
});

await client.get('https://example.com');

urllib

import { request, setGlobalDispatcher } from 'urllib';
import { CookieJar } from 'tough-cookie';
import { CookieClient } from 'http-cookie-agent/undici';

const jar = new CookieJar();
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

await request('https://example.com');

Using with another Agent library

If you want to use another Agent library, wrap the agent in createCookieAgent.

import https from 'node:https';

import { HttpsAgent as KeepAliveAgent } from 'agentkeepalive';
import { CookieJar } from 'tough-cookie';
import { createCookieAgent } from 'http-cookie-agent/http';

const Agent = createCookieAgent(KeepAliveAgent);

const jar = new CookieJar();
const agent = new Agent({ cookies: { jar } });

https.get('https://example.com', { agent }, (res) => {
  // ...
});

undici

If you want to use another undici Agent library, use CookieClient via factory function.

import { fetch, ProxyAgent } from 'undici';
import { CookieJar } from 'tough-cookie';
import { CookieClient } from 'http-cookie-agent/undici';

const jar = new CookieJar();
const agent = new ProxyAgent({
  factory: (origin, opts) => {
    return new CookieClient(origin, {
      ...opts,
      cookies: { jar },
    });
  },
});

await fetch('https://example.com', { dispatcher: agent });

If you want to use another undici Client library, wrap the client in createCookieClient.

import { fetch, Agent, MockClient } from 'undici';
import { CookieJar } from 'tough-cookie';
import { createCookieClient } from 'http-cookie-agent/undici';

const CookieClient = createCookieClient(MockClient);

const jar = new CookieJar();
const agent = new Agent({
  factory: (origin, opts) => {
    return new CookieClient(origin, {
      ...opts,
      cookies: { jar },
    });
  },
});

await fetch('https://example.com', { dispatcher: agent });

Contributing

PRs accepted.

License

MIT (c) 3846masa

changelog

6.0.8 (2024-12-12)

Bug Fixes

  • npm: update dependency agent-base to ^7.1.3 (#1136) (88f351f)

6.0.7 (2024-12-10)

Bug Fixes

  • npm: update dependency agent-base to ^7.1.2 (#1131) (068a2cc)

6.0.6 (2024-09-23)

Bug Fixes

6.0.5 (2024-05-02)

Bug Fixes

6.0.4 (2024-05-02)

Bug Fixes

  • removed async_UNSTABLE option because it is not working correctly (#874) (b5af18b)

6.0.3 (2024-04-04)

Bug Fixes

6.0.2 (2024-04-02)

Bug Fixes

  • npm: update dependency agent-base to ^7.1.1 (#837) (f3d0ca4)

6.0.1 (2024-01-11)

Bug Fixes

6.0.0 (2024-01-11)

Features

BREAKING CHANGES

  • Drop Node.js v14, v16 support

  • fix: supports undici v6.2.1

close https://github.com/3846masa/http-cookie-agent/pull/706 close https://github.com/3846masa/http-cookie-agent/issues/728

5.0.4 (2023-06-18)

Bug Fixes

  • change createCookieAgent types for http-proxy-agent v6 updates (e7d97dd)

5.0.3 (2023-06-18)

Bug Fixes

  • npm: update dependency agent-base to v7 (#505) (3c10abf)

5.0.2 (2022-12-28)

Bug Fixes

5.0.1 (2022-12-28)

Bug Fixes

5.0.0 (2022-12-26)

Bug Fixes

BREAKING CHANGES

  • supports undici v5.11.0 or above only

4.0.2 (2022-07-23)

Bug Fixes

  • docs: update dependency urllib to v3 (#187) (f7f7332)

4.0.1 (2022-05-21)

Bug Fixes

  • supports node.js global fetch since node.js v18.2.0 (#127) (05b669c)

4.0.0 (2022-05-08)

Bug Fixes

BREAKING CHANGES

  • http-cookie-agent/http:node cannot use on windows, so renamed http-cookie-agent/http

  • ci: test on windows and macos environments

  • chore: update MIGRATION.md

3.0.0 (2022-05-07)

Features

  • change import path for node:http (#105) (6260bdc)
  • update README and add MIGRATION GUIDES (#110) (cb03a9f)
  • use cookiejar synchronous functions by default (#107) (2bf68bb)

BREAKING CHANGES

  • see MIGRATION.md for more details.
  • The property name for passing cookiejar to agent has been changed.
  • Changed to use cookiejar synchronous functions by default. If you use an asynchronous cookiejar store, set cookies.async_UNSTABLE to true.
  • you should import 'http-cookie-agent/node:http' instead of 'http-cookie-agent'.

2.1.2 (2022-05-07)

Bug Fixes

  • loose the validation of CookieOptions.jar so that different versions of CookieJar can be used. (#108) (adc4b74)

2.1.1 (2022-05-07)

Bug Fixes

2.1.0 (2022-05-06)

Features

2.0.0 (2022-05-02)

Features

BREAKING CHANGES

  • drop Node.js v12 support, please use v14.18.0 or above

1.0.6 (2022-05-02)

Bug Fixes

  • send data correctly when keepalive is enabled (#90) (b245a77)

1.0.5 (2022-02-27)

Bug Fixes

  • deps: update actions/setup-node action to v3 (#66) (90a87fa)

1.0.4 (2021-12-25)

Bug Fixes

1.0.3 (2021-10-30)

Bug Fixes

  • avoid to url-encode cookie value (#27) (1c1da5c)

1.0.2 (2021-10-14)

Bug Fixes

1.0.1 (2021-09-23)

Bug Fixes

  • send cookies even when target is same host but different port (#11) (bb1b8f3)

1.0.0 (2021-09-12)

Features