HTTP Header Parser

2.0.6 · maintenance · verified Tue Apr 21

The `parse-headers` package is a lightweight utility designed to transform raw HTTP header strings into a JavaScript object. It is currently at version 2.0.6, with its last update occurring approximately one year ago (as of early 2024), indicating a slow release cadence and minimal active development. The library's core functionality involves parsing a multi-line header string, lowercasing all header names, and consolidating multiple instances of the same header into an array of values. It distinguishes itself by being a zero-dependency solution, making it ideal for environments where a minimal footprint is critical, such as older browser environments via Browserify or for basic XHR header processing. Unlike more comprehensive, RFC-strict parsers that handle structured field values, `parse-headers` offers a straightforward, object-based representation suitable for common, less complex HTTP header scenarios.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing a multi-line HTTP header string into a JavaScript object, showing how duplicate headers are handled as arrays.

import parse from 'parse-headers';

const headersString = [
  'Date: Sun, 17 Aug 2014 16:24:52 GMT',
  'Content-Type: text/html; charset=utf-8',
  'Transfer-Encoding: chunked',
  'X-Custom-Header: beep',
  'X-Custom-Header: boop',
  'Accept-Language: en-US,en;q=0.9,fr;q=0.8'
].join('\n');

const parsed = parse(headersString);

console.log(parsed);
/*
Output:
{
  date: 'Sun, 17 Aug 2014 16:24:52 GMT',
  'content-type': 'text/html; charset=utf-8',
  'transfer-encoding': 'chunked',
  'x-custom-header': [ 'beep', 'boop' ],
  'accept-language': 'en-US,en;q=0.9,fr;q=0.8'
}
*/

// Accessing a single header
console.log('Content Type:', parsed['content-type']);
// Accessing a repeated header
console.log('Custom Headers:', parsed['x-custom-header']);

view raw JSON →