{"id":20653033,"url":"https://github.com/3846masa/http-cookie-agent","last_synced_at":"2025-04-04T06:08:05.880Z","repository":{"id":37037927,"uuid":"405623348","full_name":"3846masa/http-cookie-agent","owner":"3846masa","description":"Allows cookies with every Node.js HTTP clients (e.g. Node.js global fetch, undici, axios, node-fetch).","archived":false,"fork":false,"pushed_at":"2024-10-29T13:33:55.000Z","size":3623,"stargazers_count":42,"open_issues_count":4,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-29T16:07:59.924Z","etag":null,"topics":["agent","cookie","cookies","http","https","nodejs","tough-cookie","undici"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/3846masa.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"3846masa"}},"created_at":"2021-09-12T11:28:21.000Z","updated_at":"2024-10-29T13:33:58.000Z","dependencies_parsed_at":"2023-10-03T00:31:42.351Z","dependency_job_id":"97ab8c5c-a79e-4a57-a098-79e6f9d4c049","html_url":"https://github.com/3846masa/http-cookie-agent","commit_stats":{"total_commits":491,"total_committers":4,"mean_commits":122.75,"dds":"0.13645621181262724","last_synced_commit":"35803f98f3c193336cd245c6d1bccfa986c2f80f"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3846masa%2Fhttp-cookie-agent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3846masa%2Fhttp-cookie-agent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3846masa%2Fhttp-cookie-agent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3846masa%2Fhttp-cookie-agent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/3846masa","download_url":"https://codeload.github.com/3846masa/http-cookie-agent/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247128746,"owners_count":20888235,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["agent","cookie","cookies","http","https","nodejs","tough-cookie","undici"],"created_at":"2024-11-16T17:40:44.145Z","updated_at":"2025-04-04T06:08:05.861Z","avatar_url":"https://github.com/3846masa.png","language":"TypeScript","readme":"# HTTP Cookie Agent\n\n![HTTP Cookie Agent](./docs/assets/ogp.jpg)\n\n[![github sponsors](https://flat.badgen.net/badge/GitHub%20Sponsors/Support%20me%20%E2%9D%A4/ff69b4?icon=github)](https://github.com/sponsors/3846masa)\n[![npm](https://flat.badgen.net/npm/v/http-cookie-agent)](https://www.npmjs.com/package/http-cookie-agent)\n[![license](https://flat.badgen.net/badge/license/MIT/blue)](LICENSE)\n[![standard-readme compliant](https://flat.badgen.net/badge/readme%20style/standard/green)](https://github.com/RichardLitt/standard-readme)\n\nAllows cookies with every Node.js HTTP clients (e.g. Node.js global fetch, undici, axios, node-fetch).\n\n## Table of Contents\n\n- [Install](#install)\n- [Usage](#usage)\n  - [Supported libraries](#supported-libraries)\n  - [Using with an asynchronous Cookie store](#using-with-an-asynchronous-cookie-store)\n  - [Using with another Agent library](#using-with-another-agent-library)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Install\n\n```bash\nnpm install http-cookie-agent tough-cookie\n```\n\nWhen you want to use Node.js global fetch (aka. `undici`), you should install `undici` additionally.\n\n```bash\nnpm install undici\n```\n\n## Usage\n\nSee also [examples](./examples) for more details.\n\n### Supported libraries\n\n| Library              | Supported?        |\n| -------------------- | ----------------- |\n| Node.js global fetch | ✅                |\n| `undici`             | ✅                |\n| `node:http`          | ✅                |\n| `node:https`         | ✅                |\n| `axios`              | ✅                |\n| `node-fetch`         | ✅                |\n| `got`                | ✅ \u003csup\u003e\\*1\u003c/sup\u003e |\n| `superagent`         | ✅ \u003csup\u003e\\*1\u003c/sup\u003e |\n| `request`            | ✅ \u003csup\u003e\\*1\u003c/sup\u003e |\n| `needle`             | ✅                |\n| `phin`               | ✅                |\n| `@hapi/wrech`        | ✅                |\n| `urllib`             | ✅                |\n| Bun global fetch     | ❌ \u003csup\u003e\\*2\u003c/sup\u003e |\n| Deno global fetch    | ❌ \u003csup\u003e\\*2\u003c/sup\u003e |\n\n\\*1: This library supports cookies by default. You may not need `http-cookie-agent`.\n\n\\*2: There have proprietary fetch implementation and is [not currently supported](https://github.com/3846masa/http-cookie-agent/issues/692).\n\n#### Node.js global fetch\n\n`http-cookie-agent` supports global fetch since Node.js v18.2.0.\n\n```js\nimport { CookieJar } from 'tough-cookie';\nimport { CookieAgent } from 'http-cookie-agent/undici';\n\nconst jar = new CookieJar();\nconst agent = new CookieAgent({ cookies: { jar } });\n\nawait fetch('https://example.com', { dispatcher: agent });\n```\n\n#### `undici`\n\n```js\nimport { fetch } from 'undici';\nimport { CookieJar } from 'tough-cookie';\nimport { CookieAgent } from 'http-cookie-agent/undici';\n\nconst jar = new CookieJar();\nconst agent = new CookieAgent({ cookies: { jar } });\n\nawait fetch('https://example.com', { dispatcher: agent });\n```\n\n#### `node:http` / `node:https`\n\n```js\nimport https from 'node:https';\n\nimport { CookieJar } from 'tough-cookie';\nimport { HttpsCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\nconst agent = new HttpsCookieAgent({ cookies: { jar } });\n\nhttps.get('https://example.com', { agent }, (res) =\u003e {\n  // ...\n});\n```\n\n#### `axios`\n\n```js\nimport axios from 'axios';\nimport { CookieJar } from 'tough-cookie';\nimport { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\n\nconst client = axios.create({\n  httpAgent: new HttpCookieAgent({ cookies: { jar } }),\n  httpsAgent: new HttpsCookieAgent({ cookies: { jar } }),\n});\n\nawait client.get('https://example.com');\n```\n\n#### `node-fetch`\n\n```js\nimport fetch from 'node-fetch';\nimport { CookieJar } from 'tough-cookie';\nimport { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\n\nconst httpAgent = new HttpCookieAgent({ cookies: { jar } });\nconst httpsAgent = new HttpsCookieAgent({ cookies: { jar } });\n\nawait fetch('https://example.com', {\n  agent: ({ protocol }) =\u003e {\n    return protocol === 'https:' ? httpsAgent : httpAgent;\n  },\n});\n```\n\n#### `got`\n\n:warning: `got` supports cookies by default. You may not need `http-cookie-agent`.\n\nSee https://github.com/sindresorhus/got/tree/v11.8.2#cookies.\n\n```js\nimport got from 'got';\nimport { CookieJar } from 'tough-cookie';\nimport { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\n\nconst client = got.extend({\n  agent: {\n    http: new HttpCookieAgent({ cookies: { jar } }),\n    https: new HttpsCookieAgent({ cookies: { jar } }),\n  },\n});\n\nawait client('https://example.com');\n```\n\n#### `superagent`\n\n:warning: `superagent` supports cookies by default. You may not need `http-cookie-agent`.\n\nSee https://github.com/visionmedia/superagent/blob/v6.1.0/docs/index.md#saving-cookies.\n\n```js\nimport superagent from 'superagent';\nimport { CookieJar } from 'tough-cookie';\nimport { MixedCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\nconst mixedAgent = new MixedCookieAgent({ cookies: { jar } });\n\nconst client = superagent.agent().use((req) =\u003e req.agent(mixedAgent));\n\nawait client.get('https://example.com');\n```\n\n#### `request`\n\n:warning: `request` supports cookies by default. You may not need `http-cookie-agent`.\n\nSee https://github.com/request/request/tree/v2.88.1#examples.\n\n```js\nimport request from 'request';\nimport { CookieJar } from 'tough-cookie';\nimport { MixedCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\n\nconst client = request.defaults({\n  agent: new MixedCookieAgent({ cookies: { jar } }),\n});\n\nclient.get('https://example.com', (_err, _res) =\u003e {\n  // ...\n});\n```\n\n#### `needle`\n\n```js\nimport needle from 'needle';\nimport { CookieJar } from 'tough-cookie';\nimport { MixedCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\n\nawait needle('get', 'https://example.com', {\n  agent: new MixedCookieAgent({ cookies: { jar } }),\n});\n```\n\n#### `phin`\n\n```js\nimport phin from 'phin';\nimport { CookieJar } from 'tough-cookie';\nimport { MixedCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\n\nawait phin({\n  url: 'https://example.com',\n  core: {\n    agent: new MixedCookieAgent({ cookies: { jar } }),\n  },\n});\n```\n\n#### `@hapi/wreck`\n\n```js\nimport Wreck from '@hapi/wreck';\nimport { CookieJar } from 'tough-cookie';\nimport { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';\n\nconst jar = new CookieJar();\n\nconst client = Wreck.defaults({\n  agents: {\n    http: new HttpCookieAgent({ cookies: { jar } }),\n    https: new HttpsCookieAgent({ cookies: { jar } }),\n    httpsAllowUnauthorized: new HttpsCookieAgent({ cookies: { jar } }),\n  },\n});\n\nawait client.get('https://example.com');\n```\n\n#### `urllib`\n\n```js\nimport { request, setGlobalDispatcher } from 'urllib';\nimport { CookieJar } from 'tough-cookie';\nimport { CookieClient } from 'http-cookie-agent/undici';\n\nconst jar = new CookieJar();\nconst agent = new CookieAgent({ cookies: { jar } });\nsetGlobalDispatcher(agent);\n\nawait request('https://example.com');\n```\n\n### Using with another Agent library\n\nIf you want to use another Agent library, wrap the agent in `createCookieAgent`.\n\n```js\nimport https from 'node:https';\n\nimport { HttpsAgent as KeepAliveAgent } from 'agentkeepalive';\nimport { CookieJar } from 'tough-cookie';\nimport { createCookieAgent } from 'http-cookie-agent/http';\n\nconst Agent = createCookieAgent(KeepAliveAgent);\n\nconst jar = new CookieJar();\nconst agent = new Agent({ cookies: { jar } });\n\nhttps.get('https://example.com', { agent }, (res) =\u003e {\n  // ...\n});\n```\n\n#### `undici`\n\nIf you want to use another undici Agent library, use `CookieClient` via factory function.\n\n```js\nimport { fetch, ProxyAgent } from 'undici';\nimport { CookieJar } from 'tough-cookie';\nimport { CookieClient } from 'http-cookie-agent/undici';\n\nconst jar = new CookieJar();\nconst agent = new ProxyAgent({\n  factory: (origin, opts) =\u003e {\n    return new CookieClient(origin, {\n      ...opts,\n      cookies: { jar },\n    });\n  },\n});\n\nawait fetch('https://example.com', { dispatcher: agent });\n```\n\nIf you want to use another undici Client library, wrap the client in `createCookieClient`.\n\n```js\nimport { fetch, Agent, MockClient } from 'undici';\nimport { CookieJar } from 'tough-cookie';\nimport { createCookieClient } from 'http-cookie-agent/undici';\n\nconst CookieClient = createCookieClient(MockClient);\n\nconst jar = new CookieJar();\nconst agent = new Agent({\n  factory: (origin, opts) =\u003e {\n    return new CookieClient(origin, {\n      ...opts,\n      cookies: { jar },\n    });\n  },\n});\n\nawait fetch('https://example.com', { dispatcher: agent });\n```\n\n## Contributing\n\nPRs accepted.\n\n## License\n\n[MIT (c) 3846masa](../LICENSE)\n","funding_links":["https://github.com/sponsors/3846masa"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3846masa%2Fhttp-cookie-agent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F3846masa%2Fhttp-cookie-agent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3846masa%2Fhttp-cookie-agent/lists"}