How to fix duplicate response headers in Puppeteer

Puppeteer

Summary

  • Duplicate header values should normally be merged into a single header value separated by a comma and a space. The Set-Cookie header is an exception and remains as separate values.
  • A fix was implemented to normalize newline-separated duplicate headers into comma-separated format, aligning with common HTTP and Fetch API expectations.

What changed

  • Headers that appear multiple times are now merged into one header with comma-separated values.
  • Set-Cookie headers remain distinct per their semantics.

How to use / post-process headers (example)

function normalizeHeaders(headers) {
  const out = {};
  for (const [key, value] of Object.entries(headers)) {
    const k = key.toLowerCase();
    if (k === 'set-cookie') {
      out[key] = Array.isArray(value) ? value : [value];
    } else {
      if (Array.isArray(value)) {
        out[key] = value.join(', ');
      } else {
        out[key] = value;
      }
    }
  }
  return out;
}

Example result after normalization

{
  "server-timing": "sis; desc=0, geo; desc=IN, ak_p; desc=\"1760514732612_1750730254_2156868_116_7771_4_9_255\";dur=1",
  "set-cookie": ["AKA_A2=A; expires=Wed, 15-Oct-2025 08:59:55 GMT; path=/; domain=adobe.com; secure; HttpOnly"],
  "content-length": "3876",
  ...
}

Notes

  • This change resolves issues caused by attempting to treat newline-separated headers as separate fields in header maps.
  • If you are consuming header data programmatically, prefer the normalized form where duplicates are merged with comma-space separators, except for Set-Cookie which remains as individual values.