HTTP Status Code Utilities
The `http-status-code` package provides a straightforward, protocol-aware utility for retrieving human-readable messages associated with HTTP status codes. Currently at version 2.1.0, this library does not indicate a rapid release cadence, suggesting a stable, mature, and low-maintenance tool. Its primary differentiation lies in allowing developers to specify the HTTP protocol version (e.g., HTTP/1.0, HTTP/1.1, HTTP/2) when querying for a status message, ensuring accuracy for protocol-specific codes or messages. If no protocol is specified, it defaults to a comprehensive set. It's a simple, focused solution for translating numeric status codes into their standard descriptions.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'getMessage')
cause Attempting to call `getMessage` on an undefined `HTTPStatusCode` object, likely due to incorrect CommonJS import syntax.fixEnsure `HTTPStatusCode` is correctly imported: `const HTTPStatusCode = require('http-status-code');` in CommonJS or `import HTTPStatusCode from 'http-status-code';` in ESM. -
TypeError: (0 , http_status_code_1.getMessage) is not a function
cause Incorrectly trying to destructure `getMessage` as a named export from an ESM import, when it's a method of the default export.fixAccess `getMessage` as a method of the default import: `import HTTPStatusCode from 'http-status-code'; HTTPStatusCode.getMessage(200);`
Warnings
- gotcha The `getMessage` function returns 'Unknown' if the status code or protocol combination is not found. There is no explicit error thrown, which might mask issues if expecting specific messages.
- gotcha The package primarily uses CommonJS `require` syntax in its examples. While modern tools can often consume it with ESM `import`, ensure your build pipeline correctly handles CommonJS module interoperability if you encounter import issues in an ESM-only environment.
- gotcha Protocol names are case-sensitive and must match the internal definitions (e.g., 'HTTP/1.1', 'HTTP/2'). Providing an incorrectly cased or non-existent protocol name will result in an 'Unknown' message or an empty list of status codes.
Install
-
npm install http-status-code -
yarn add http-status-code -
pnpm add http-status-code
Imports
- HTTPStatusCode
const HTTPStatusCode = require('http-status-code').HTTPStatusCode;import HTTPStatusCode from 'http-status-code';
- getMessage
import { getMessage } from 'http-status-code';import HTTPStatusCode from 'http-status-code'; const message = HTTPStatusCode.getMessage(200);
- All HTTP/1.1 Status Codes
import { HTTP11_CODES } from 'http-status-code';import HTTPStatusCode from 'http-status-code'; const codes = HTTPStatusCode.getProtocolStatusCodes('HTTP/1.1');
Quickstart
import HTTPStatusCode from 'http-status-code';
// Get the message for a common status code without specifying a protocol
console.log('HTTP 200 message:', HTTPStatusCode.getMessage(200));
// Get the message for a status code specific to HTTP/1.1
console.log('HTTP/1.1 429 message:', HTTPStatusCode.getMessage(429, 'HTTP/1.1'));
// Get the message for a status code that might differ or be unknown in a specific protocol
console.log('WEBDAV 429 message (example of unknown):', HTTPStatusCode.getMessage(429, 'WEBDAV'));
// Retrieve all status codes for a specific protocol
const http2Codes = HTTPStatusCode.getProtocolStatusCodes('HTTP/2');
console.log('\nHTTP/2 status codes (first 5):', Object.entries(http2Codes).slice(0, 5));