{"id":13311,"library":"http-message-parser","title":"HTTP Message Parser","description":"http-message-parser is a JavaScript library designed to parse raw HTTP message strings or Buffers into structured JavaScript objects. It handles both request and response messages, including multipart bodies, and aims to retain original encodings by returning body content as Node.js Buffer objects. The current stable version is 0.0.34, released in 2018. Due to its age, it primarily targets older Node.js environments (engines >=0.10.0) and uses CommonJS modules. A key differentiator is its focus on binary data integrity, performing offset slices on input buffers to avoid stringifying binary content, making it suitable for parsing responses with non-textual data.","status":"abandoned","version":"0.0.34","language":"javascript","source_language":"en","source_url":"https://github.com/miguelmota/http-message-parser","tags":["javascript","http","message","parser","request","response","protocol","format","cli"],"install":[{"cmd":"npm install http-message-parser","lang":"bash","label":"npm"},{"cmd":"yarn add http-message-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-message-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This library is primarily CommonJS and does not officially support ESM. Direct ESM import might fail or require a CommonJS wrapper.","wrong":"import httpMessageParser from 'http-message-parser';","symbol":"httpMessageParser","correct":"const httpMessageParser = require('http-message-parser');"},{"note":"The default export (or module.exports) is the parsing function itself.","symbol":"parseHttpMessage","correct":"const parseHttpMessage = require('http-message-parser');"},{"note":"The package also exposes a command-line interface, not typically imported as a symbol but used via `http-message-parser <file>` or piping input.","symbol":"httpMessageParserCli","correct":"# CLI usage as shown in documentation"}],"quickstart":{"code":"const httpMessageParser = require('http-message-parser');\nconst fs = require('fs');\nconst path = require('path');\n\nconst exampleMessage = [\n  'HTTP/1.1 200 OK',\n  'MIME-Version: 1.0',\n  'Content-Type: multipart/mixed; boundary=frontier',\n  '',\n  'This is a message with multiple parts in MIME format.',\n  '--frontier',\n  'Content-Type: text/plain',\n  '',\n  'This is the body of the message.',\n  '--frontier',\n  'Content-Type: application/octet-stream',\n  'Content-Transfer-Encoding: base64',\n  '',\n  'PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg',\n  'Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==',\n  '--frontier--'\n].join('\\r\\n');\n\n// Simulate reading from a file or network stream with a Buffer\nconst messageBuffer = Buffer.from(exampleMessage, 'binary');\n\nconsole.log('Parsing HTTP message...');\nconst parsedMessage = httpMessageParser(messageBuffer);\n\nconsole.log(JSON.stringify(parsedMessage, (key, value) => {\n  // Convert Buffer to string for display, or indicate its presence\n  if (value && typeof value === 'object' && value.type === 'Buffer') {\n    return `<Buffer(${value.data.length} bytes)>`;\n  }\n  return value;\n}, 2));\n\n// Accessing specific parts\nif (parsedMessage.multipart && parsedMessage.multipart.length > 0) {\n  const firstPartBody = parsedMessage.multipart[0].body;\n  console.log('\\nFirst multipart body (decoded):', firstPartBody.toString('utf8'));\n}","lang":"javascript","description":"This quickstart demonstrates parsing a multi-part HTTP response from a Buffer and then inspecting its structured output, including decoding one of the multipart bodies."},"warnings":[{"fix":"Consider alternative, actively maintained HTTP parsing libraries for modern Node.js applications or thoroughly test compatibility before production deployment. Ensure a CommonJS environment.","message":"The library primarily targets Node.js versions >=0.10.0 (circa 2012) and has not been updated since 2018. It may not be fully compatible with modern Node.js features, ES modules, or recent changes in HTTP standards or Buffer API behavior.","severity":"breaking","affected_versions":">=0.0.35 (hypothetical), or running on Node.js >=12"},{"fix":"Always check `typeof body === 'object' && body.type === 'Buffer'` and use `body.toString('encoding')` or other Buffer methods as appropriate. In browser environments, a `Buffer` polyfill like `feross/buffer` is required for binary data handling.","message":"All message bodies (main body and multipart bodies) are returned as Node.js Buffer objects to preserve original encoding. This requires explicit conversion to string (e.g., `body.toString('utf8')`) if text is expected, or special handling for binary data.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Use CommonJS `require()` for importing the library. If absolutely necessary in an ESM project, use dynamic `import()` or review Node.js's CJS-ESM interop documentation.","message":"The package uses `require()` and does not explicitly support ES Modules (ESM). Attempting to `import` it directly in an ESM context might lead to errors or unexpected behavior unless specific Node.js interop flags are used.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are using `const httpMessageParser = require('http-message-parser');` in a CommonJS module or environment.","cause":"Attempting to import the module incorrectly in an ESM context or trying to destructure a non-object export.","error":"httpMessageParser is not a function"},{"fix":"Install and configure a `Buffer` polyfill (e.g., `npm install buffer` and import it) for browser environments when dealing with binary data.","cause":"Using the library in a browser environment without providing a `Buffer` polyfill.","error":"Buffer is not defined"},{"fix":"Always check the `parsedMessage` object and its sub-properties (e.g., `parsedMessage && parsedMessage.headers`) for existence before accessing them, especially when handling arbitrary or potentially invalid input.","cause":"Attempting to access properties like 'headers' or 'body' on a `parsedMessage` object that might be `null` or have an unexpected structure if the input message was malformed or empty.","error":"TypeError: Cannot read properties of undefined (reading 'headers')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}