HTTP Header Parser
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
-
ReferenceError: require is not defined
cause Attempting to use `require('parse-headers')` in a JavaScript file that is being interpreted as an ES Module (e.g., due to `"type": "module"` in `package.json` or `.mjs` file extension).fixChange the import statement to `import parse from 'parse-headers';` for ES Module compatibility. If strictly needing CommonJS, ensure the file is treated as CommonJS (e.g., `.cjs` extension or no `"type": "module"` in `package.json`). -
TypeError: (0 , parse_headers__WEBPACK_IMPORTED_MODULE_0__.parse) is not a function
cause This error typically occurs in a bundled environment when `parse-headers` (which exports a default function) is imported as a named export (`import { parse } from 'parse-headers'`).fixCorrect the import statement to use a default import: `import parse from 'parse-headers';` -
Unexpected single string instead of an array for a repeated header
cause Developers might mistakenly expect a single string value for a header like 'X-Custom-Header' if it appeared multiple times in the input, not realizing the library combines them into an array.fixAlways anticipate that repeated HTTP headers will be represented as an array of strings in the parsed object. Implement logic to handle both single string (for non-repeated headers) and array (for repeated headers) outcomes, or explicitly check `Array.isArray()` before processing. Example: `const values = Array.isArray(parsed['header-name']) ? parsed['header-name'] : [parsed['header-name']];`
Warnings
- gotcha The `parse-headers` package has seen minimal updates since its initial release in 2014, with its last publish being over a year ago. While functional for basic use cases, users should be aware that active feature development, support for new HTTP RFCs, or prompt bug/security fixes are unlikely.
- gotcha This parser always converts HTTP header names to lowercase in the resulting JavaScript object. If your application relies on the original casing of header names, or if the order of duplicate headers is semantically important beyond their collection into an array, this behavior must be explicitly accounted for.
- gotcha For headers that appear multiple times in the input string (e.g., `Set-Cookie` or custom headers), `parse-headers` consolidates all values into an array. If only the first or last instance is expected, explicit handling of the array is necessary.
Install
-
npm install parse-headers -
yarn add parse-headers -
pnpm add parse-headers
Imports
- parse
import { parse } from 'parse-headers'import parse from 'parse-headers'
- parse
import parse from 'parse-headers'
const parse = require('parse-headers') - ParsedHeaders
import type { ParsedHeaders } from 'parse-headers'
Quickstart
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']);