{"id":13312,"library":"http-z","title":"HTTP-Z Message Parser and Builder","description":"http-z is a focused JavaScript and TypeScript library for parsing and building HTTP messages (requests and responses) in strict accordance with RFC 7230. It provides distinct functions to convert raw HTTP strings into a structured model and to serialize a model back into a raw message string. The current stable version is 8.1.1, indicating ongoing maintenance and development. A key differentiator for http-z is its zero-dependency design, which contributes to a minimal footprint, making it suitable for environments where bundle size is critical. It operates seamlessly across both Node.js (requiring Node.js >=18) and modern web browser environments. This library is ideal for developers needing low-level interaction with HTTP message serialization and deserialization, offering a robust, RFC-compliant alternative to higher-level HTTP client or server frameworks by concentrating solely on the message structure.","status":"active","version":"8.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/AlexanderMac/http-z","tags":["javascript","http","http-utility","http-message","builder","parser","request","response","typescript"],"install":[{"cmd":"npm install http-z","lang":"bash","label":"npm"},{"cmd":"yarn add http-z","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-z","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"http-z is primarily designed for ESM environments. While CommonJS might work with transpilation, direct `require` for named exports is generally not supported in Node.js >=18 when the package is ESM-first.","wrong":"const { parse } = require('http-z')","symbol":"parse","correct":"import { parse } from 'http-z'"},{"note":"Both `parse` and `build` are named exports, not default exports.","wrong":"import build from 'http-z'","symbol":"build","correct":"import { build } from 'http-z'"},{"note":"This is a TypeScript type import for the parsed HTTP message structure.","symbol":"HttpZParserModel","correct":"import type { HttpZParserModel } from 'http-z'"},{"note":"The library exports a `consts` object containing various HTTP constants (methods, headers, etc.). It's a named export, not a wildcard import of everything.","wrong":"import * as consts from 'http-z'","symbol":"consts","correct":"import { consts } from 'http-z'"}],"quickstart":{"code":"import { parse, build } from 'http-z'\n\nconst plainRequestMessage = [\n  'GET /api/data?param1=value1 HTTP/1.1',\n  'Host: example.com',\n  'User-Agent: http-z-client/1.0',\n  'Accept: application/json',\n  'Content-Length: 0',\n  '',\n  ''\n].join('\\r\\n')\n\nconsole.log('--- Original Request ---')\nconsole.log(plainRequestMessage)\n\ntry {\n  const messageModel = parse(plainRequestMessage)\n  console.log('\\n--- Parsed Model ---')\n  console.log(JSON.stringify(messageModel, null, 2))\n\n  // Example of modifying the model\n  const modifiedModel = {\n    ...messageModel,\n    method: 'POST',\n    path: '/api/submit',\n    queryParams: [],\n    headers: [\n      ...messageModel.headers.filter(h => h.name.toLowerCase() !== 'content-length'),\n      { name: 'Content-Type', value: 'application/x-www-form-urlencoded' },\n      { name: 'Content-Length', value: '11' }\n    ],\n    body: 'key=value'\n  }\n\n  const rebuiltMessage = build(modifiedModel)\n  console.log('\\n--- Rebuilt Message ---')\n  console.log(rebuiltMessage)\n\n} catch (error) {\n  console.error('\\nError parsing/building message:', error.message)\n}","lang":"typescript","description":"This quickstart demonstrates parsing a raw HTTP GET request, inspecting its structured model, modifying the model to represent a POST request, and then rebuilding the raw HTTP message from the modified model."},"warnings":[{"fix":"Upgrade your Node.js environment to version 18 or newer to ensure compatibility.","message":"http-z requires Node.js version 18 or higher. Projects running on older Node.js runtimes will encounter errors.","severity":"breaking","affected_versions":"<8.0.0"},{"fix":"Ensure input messages are strictly RFC 7230 compliant. Use robust error handling around `parse` calls to catch and log specific parsing failures.","message":"The parser strictly adheres to RFC 7230. Malformed or non-standard HTTP messages may lead to parsing errors (e.g., 'Error: Invalid HTTP message format') where other, more lenient parsers might succeed.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Manually update the `Content-Length` header in the message model to match the byte length of the `body` string, especially after modifying the body content.","message":"When rebuilding messages, ensure the `Content-Length` header accurately reflects the `body` length. Mismatches can lead to unexpected behavior in HTTP clients/servers. `http-z` does not automatically calculate or correct `Content-Length`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`) and use `import { parse, build } from 'http-z'` syntax. If staying in CommonJS, you might need a transpiler or a dynamic import `import('http-z').then(({ parse, build }) => ...)`.","cause":"Attempting to use `require()` to import `http-z` in a CommonJS module while the package is designed for ESM, leading to conflicts in module export mechanisms.","error":"TypeError: Cannot assign to read only property 'exports' of object '#<Object>'"},{"fix":"Use the correct named import syntax: `import { parse, build } from 'http-z'`. Verify your Node.js environment supports ESM or use a bundler/transpiler if targeting older environments.","cause":"Incorrectly trying to `require` a named export from an ESM-only package as if it were a CommonJS module, or trying to use `import parse from 'http-z'` (default import).","error":"SyntaxError: Named export 'parse' not found. The requested module 'http-z' does not provide an export named 'parse'"},{"fix":"Review the `rawMessage` string for common HTTP formatting issues: incorrect line endings (must be `\\r\\n`), missing blank line between headers and body, malformed status lines or headers, or invalid characters. Ensure all parts of the HTTP message are correctly structured.","cause":"The input string provided to the `parse` function does not conform to the strict HTTP/1.1 message format as defined by RFC 7230.","error":"Error: Invalid HTTP message format"},{"fix":"Verify that `parse` is imported as a named export: `import { parse } from 'http-z'`. Avoid `import httpz from 'http-z'` as there is no default export for the main functions.","cause":"The `parse` function was not correctly imported or is being accessed incorrectly from the imported module. This often happens with incorrect default vs. named import syntax.","error":"TypeError: parse is not a function"}],"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}