Set-Cookie Header Parser

3.1.0 · active · verified Tue Apr 21

set-cookie-parser is a JavaScript/TypeScript library designed to parse `Set-Cookie` HTTP response headers into structured JavaScript objects. It currently stands at stable version 3.1.0 and is actively maintained, with a healthy release cadence and recent updates. The library is highly versatile, accepting various inputs including single header strings, arrays of header strings, Node.js `http.ServerResponse` objects, and `fetch()` `Response` objects. A key differentiator is its ability to return either an array of cookie objects or a map where cookie names are keys, depending on configuration. Each parsed cookie object provides a comprehensive set of attributes, including `name`, `value`, `path`, `domain`, `expires` (as `Date` objects), `maxAge`, `secure`, `httpOnly`, `sameSite`, and `partitioned`. It ships with built-in TypeScript types, ensuring robust type checking and improved developer experience in modern TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse `Set-Cookie` headers from an HTTP response object, iterating and logging the properties of each parsed cookie.

import * as http from 'node:http';
import { parseSetCookie } from 'set-cookie-parser';

// Example: Fetching from a URL and parsing Set-Cookie headers
http.get('http://example.com', function(res) {
  const cookies = parseSetCookie(res, {
    decodeValues: true  // default: true
  });

  console.log('Parsed Cookies from example.com:');
  if (cookies.length === 0) {
    console.log('No Set-Cookie headers found.');
  } else {
    cookies.forEach(cookie => {
      console.log(`- Name: ${cookie.name}, Value: ${cookie.value}`);
      if (cookie.expires) console.log(`  Expires: ${cookie.expires.toISOString()}`);
      if (cookie.maxAge) console.log(`  Max-Age: ${cookie.maxAge} seconds`);
      if (cookie.domain) console.log(`  Domain: ${cookie.domain}`);
      if (cookie.path) console.log(`  Path: ${cookie.path}`);
      if (cookie.secure) console.log(`  Secure: true`);
      if (cookie.httpOnly) console.log(`  HttpOnly: true`);
      if (cookie.sameSite) console.log(`  SameSite: ${cookie.sameSite}`);
      if (cookie.partitioned) console.log(`  Partitioned: true`);
      console.log('---');
    });
  }
}).on('error', (e) => {
  console.error(`Error fetching: ${e.message}`);
});

view raw JSON →