Skip to content

Instantly share code, notes, and snippets.

@montasim
Last active January 28, 2025 08:13
Show Gist options
  • Save montasim/4288c39b90b51e9f0dbc663d77db68e5 to your computer and use it in GitHub Desktop.
Save montasim/4288c39b90b51e9f0dbc663d77db68e5 to your computer and use it in GitHub Desktop.
Defines common HTTP status codes for use in API responses. These status codes represent various states of HTTP responses and are commonly used to communicate the result of API requests. Adjust these codes as needed to align with the APIs response requirements.
/**
* @fileoverview Defines common HTTP status codes for use in API responses.
* These status codes represent various states of HTTP responses and are
* commonly used to communicate the result of API requests.
* Adjust these codes as needed to align with the API's response requirements.
*
* @module constants/httpStatus
* @version 1.0.0
* @license CC BY-NC-ND 4.0
*
* @contact Mohammad Montasim-Al-Mamun Shuvo
* @created 2025-01-28
* @contactEmail [email protected]
* @contactGithub https://github.com/montasim
*/
/**
* An object representing different HTTP status codes.
* @enum {number}
*/
const httpStatusConstants = Object.freeze({
/** Informational responses (100–199) */
/**
* Continue - The initial part of a request has been received and has not yet been rejected by the server.
* Example: The client uploads a large file in chunks; the server responds with 100 Continue after receiving the headers.
* @type {number}
*/
CONTINUE: 100,
/**
* Switching Protocols - The server understands and is willing to comply with the client's request to switch protocols.
* Example: The client requests to upgrade the connection to WebSockets, and the server agrees.
* @type {number}
*/
SWITCHING_PROTOCOLS: 101,
/**
* Processing - The server has received and is processing the request, but no response is available yet.
* Example: A WebDAV request involving multiple sub-requests is being processed, and the server informs the client to wait.
* @type {number}
*/
PROCESSING: 102,
/**
* Early Hints - Used to return some response headers before the final HTTP message.
* Example: The server sends preliminary headers (e.g., `Link` for preloading resources) while the full response is prepared.
* @type {number}
*/
EARLY_HINTS: 103,
/** Successful responses (200–299) */
/**
* OK - The request has succeeded.
* Example: A GET request for a user's profile returns the profile data successfully.
* @type {number}
*/
OK: 200,
/**
* Created - The request has been fulfilled, resulting in the creation of a new resource.
* Example: A POST request to create a new user account returns a 201 status after successfully creating the user.
* @type {number}
*/
CREATED: 201,
/**
* Accepted - The request has been accepted for processing, but the processing has not been completed.
* Example: A POST request to start a long-running job (e.g., video processing) returns 202 to indicate it was accepted.
* @type {number}
*/
ACCEPTED: 202,
/**
* Non-Authoritative Information - The request was successful, but the enclosed payload has been modified.
* Example: A caching proxy modifies the response data before returning it to the client.
* @type {number}
*/
NON_AUTHORITATIVE_INFORMATION: 203,
/**
* No Content - The server successfully processed the request, but is not returning any content.
* Example: A DELETE request to remove a resource (e.g., a user) returns a 204 status.
* @type {number}
*/
NO_CONTENT: 204,
/**
* Reset Content - The server processed the request and requires the requester to reset the document view.
* Example: A PUT request to update a form returns 205, instructing the client to reset the form.
* @type {number}
*/
RESET_CONTENT: 205,
/**
* Partial Content - The server is delivering only part of the resource due to a range header sent by the client.
* Example: A video player requests a specific byte range of a video file.
* @type {number}
*/
PARTIAL_CONTENT: 206,
/**
* Multi-Status - The message body contains multiple separate response codes for sub-requests.
* Example: A WebDAV request that modifies multiple resources returns statuses for each resource.
* @type {number}
*/
MULTI_STATUS: 207,
/**
* Already Reported - Members of a DAV binding have already been enumerated and are not included again.
* Example: A WebDAV server indicates that a resource has already been reported in a previous response.
* @type {number}
*/
ALREADY_REPORTED: 208,
/**
* IM Used - The server has fulfilled a GET request with instance manipulations applied to the resource.
* Example: A response that reflects partial updates to the original resource's state.
* @type {number}
*/
IM_USED: 226,
/** Redirection messages (300–399) */
/**
* Multiple Choices - Indicates multiple options for the resource from which the client may choose.
* Example: A request for a resource has multiple versions (e.g., different languages or formats).
* @type {number}
*/
MULTIPLE_CHOICES: 300,
/**
* Moved Permanently - This and all future requests should be directed to the given URI.
* Example: A resource's URL has changed permanently; the server responds with a new URL.
* @type {number}
*/
MOVED_PERMANENTLY: 301,
/**
* Found - Tells the client to look at another URL.
* Example: A POST request for login redirects the user to their dashboard with a temporary URL.
* @type {number}
*/
FOUND: 302,
/**
* See Other - The response to the request can be found under another URI using the GET method.
* Example: After form submission, the client is redirected to a "thank you" page.
* @type {number}
*/
SEE_OTHER: 303,
/**
* Not Modified - Indicates that the resource has not been modified since the version specified in the request headers.
* Example: A browser sends an `If-Modified-Since` header, and the server responds with 304 if the resource is unchanged.
* @type {number}
*/
NOT_MODIFIED: 304,
/**
* Use Proxy - The requested resource is only available through a proxy, whose address is provided in the response.
* Example: A client must use a proxy server to access certain restricted resources.
* @type {number}
*/
USE_PROXY: 305,
/**
* Temporary Redirect - The server is currently responding to the request with a URI for a different resource.
* Example: A temporary move of a resource to another URL for maintenance purposes.
* @type {number}
*/
TEMPORARY_REDIRECT: 307,
/**
* Permanent Redirect - The request and all future requests should use another URI.
* Example: A domain change where all requests are permanently redirected to the new domain.
* @type {number}
*/
PERMANENT_REDIRECT: 308,
/** Client error responses (400–499) */
/**
* Bad Request - The server cannot process the request due to a client error.
* Example: A POST request with invalid JSON payload returns 400.
* @type {number}
*/
BAD_REQUEST: 400,
/**
* Unauthorized - The client must authenticate itself to get the requested response.
* Example: A request to access a protected resource without a valid token.
* @type {number}
*/
UNAUTHORIZED: 401,
/**
* Payment Required - Reserved for future use.
* Example: Intended for use as a payment gateway or paywall indication.
* @type {number}
*/
PAYMENT_REQUIRED: 402,
/**
* Forbidden - The client does not have access rights to the content.
* Example: A user tries to access an admin-only endpoint without sufficient permissions.
* @type {number}
*/
FORBIDDEN: 403,
/**
* Not Found - The server cannot find the requested resource.
* Example: A GET request for a nonexistent page returns 404.
* @type {number}
*/
NOT_FOUND: 404,
/**
* Method Not Allowed - The request method is known by the server but not supported for the target resource.
* Example: A PUT request on a read-only resource returns 405.
* @type {number}
*/
METHOD_NOT_ALLOWED: 405,
/**
* Not Acceptable - The server cannot produce a response matching the Accept headers of the request.
* Example: A client requests a resource in a format the server does not support.
* @type {number}
*/
NOT_ACCEPTABLE: 406,
/**
* Proxy Authentication Required - The client must authenticate itself with the proxy.
* Example: A client accessing a resource through a proxy must provide valid credentials.
* @type {number}
*/
PROXY_AUTHENTICATION_REQUIRED: 407,
/**
* Request Timeout - The server timed out waiting for the client to send a request.
* Example: A client with an unstable connection fails to send a complete request within the server's timeout period.
* @type {number}
*/
REQUEST_TIMEOUT: 408,
/**
* Conflict - The request could not be processed due to a conflict with the current state of the resource.
* Example: A version conflict occurs when editing a resource concurrently.
* @type {number}
*/
CONFLICT: 409,
/**
* Gone - The resource requested is no longer available and no forwarding address is known.
* Example: A URL for a deleted blog post returns 410.
* @type {number}
*/
GONE: 410,
/**
* Length Required - The server requires the Content-Length header to be specified in the request.
* Example: A POST request missing a Content-Length header is rejected with 411.
* @type {number}
*/
LENGTH_REQUIRED: 411,
/**
* Precondition Failed - One or more preconditions given in the request headers were not met.
* Example: A conditional GET request with an `If-Match` header fails because the ETag does not match.
* @type {number}
*/
PRECONDITION_FAILED: 412,
/**
* URI Too Long - The URI provided by the client is too long for the server to process.
* Example: A client sends a GET request with a query string that exceeds the server's maximum URI length.
* @type {number}
*/
URI_TOO_LONG: 414,
/**
* Unsupported Media Type - The server refuses to accept the request because the payload format is unsupported.
* Example: A POST request with a content type of `application/xml` when the server only accepts `application/json`.
* @type {number}
*/
UNSUPPORTED_MEDIA_TYPE: 415,
/**
* Range Not Satisfiable - The client requested a range of the resource that cannot be satisfied.
* Example: A video player requests bytes outside the range of a video file.
* @type {number}
*/
RANGE_NOT_SATISFIABLE: 416,
/**
* Expectation Failed - The server cannot meet the requirements of the `Expect` request-header field.
* Example: A client sends an `Expect: 100-continue` header, but the server cannot honor it.
* @type {number}
*/
EXPECTATION_FAILED: 417,
/**
* I'm a teapot - A joke status code defined in RFC 2324.
* Example: Sent when a teapot is requested to brew coffee (humorous response).
* @type {number}
*/
IM_A_TEAPOT: 418,
/**
* Authentication Timeout - Indicates that the session or authentication has expired.
* Example: Used in Laravel to indicate that the user's authentication token has expired.
* @type {number}
*/
AUTHENTICATION_TIMEOUT: 419,
/**
* Misdirected Request - The request was directed at a server that cannot produce a response.
* Example: A client sends a request to the wrong server due to misconfiguration.
* @type {number}
*/
MISDIRECTED_REQUEST: 421,
/**
* Unprocessable Entity - The server understands the request but cannot process it due to semantic errors.
* Example: A POST request with invalid input data for a user registration form.
* @type {number}
*/
UNPROCESSABLE_ENTITY: 422,
/**
* Locked - The requested resource is currently locked and cannot be accessed.
* Example: A WebDAV resource is locked by another operation.
* @type {number}
*/
LOCKED: 423,
/**
* Failed Dependency - The request failed due to a failure of a dependent request.
* Example: A WebDAV request to delete a resource fails because it is locked.
* @type {number}
*/
FAILED_DEPENDENCY: 424,
/**
* Too Early - The server is unwilling to process a request that might be replayed.
* Example: A request is rejected because it was sent too soon after another similar request.
* @type {number}
*/
TOO_EARLY: 425,
/**
* Upgrade Required - The client should switch to a different protocol, such as HTTPS.
* Example: The server requires a client to upgrade to HTTP/2 or TLS.
* @type {number}
*/
UPGRADE_REQUIRED: 426,
/**
* Too Many Requests - The client has exceeded the request rate limit.
* Example: A user hits an API rate limit after sending too many requests in a short period.
* A DDoS protection mechanism throttles excessive requests with a 429 response.
* @type {number}
*/
TOO_MANY_REQUESTS: 429,
/**
* Request Header Fields Too Large - The server refuses to process the request due to large header fields.
* Example: A request with overly large cookies or authorization headers.
* @type {number}
*/
REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
/**
* No Response - Used by Nginx to indicate the server closed the connection without sending a response.
* Example: A client sends a request, but the server deliberately drops the connection.
* @type {number}
*/
NO_RESPONSE: 444,
/**
* Blocked by Windows Parental Controls - Indicates the requested resource is blocked by parental control settings.
* Example: A Windows system restricts access to certain websites or resources based on parental controls.
* @type {number}
*/
BLOCKED_BY_PARENTAL_CONTROLS: 450,
/**
* Unavailable For Legal Reasons - The server cannot provide the requested resource due to legal restrictions.
* Example: A website blocked by a government or DMCA takedown request.
* @type {number}
*/
UNAVAILABLE_FOR_LEGAL_REASONS: 451,
/**
* Client Closed Request - Used by Nginx to indicate the client closed the connection before the server could respond.
* Example: A client cancels a request before receiving the full response from the server.
* @type {number}
*/
CLIENT_CLOSED_REQUEST: 499,
/** Server error responses (500–599) */
/**
* Internal Server Error - A generic error when the server encounters an unexpected condition.
* Example: A database connection error causes the server to return 500.
* @type {number}
*/
INTERNAL_SERVER_ERROR: 500,
/**
* Bad Gateway - The server received an invalid response from the upstream server.
* Example: A gateway server cannot connect to an upstream service.
* @type {number}
*/
BAD_GATEWAY: 502,
/**
* Service Unavailable - The server is not ready to handle the request.
* Example: The server is overloaded or down for maintenance and responds with 503.
* @type {number}
*/
SERVICE_UNAVAILABLE: 503,
/**
* Gateway Timeout - The server acting as a gateway did not receive a response in time.
* Example: A request to a third-party API times out and returns 504.
* @type {number}
*/
GATEWAY_TIMEOUT: 504,
/**
* HTTP Version Not Supported - The server does not support the HTTP protocol version used in the request.
* Example: A client attempts to use HTTP/1.0, but the server requires HTTP/2.
* @type {number}
*/
HTTP_VERSION_NOT_SUPPORTED: 505,
/**
* Variant Also Negotiates - Transparent content negotiation leads to a circular reference.
* Example: A server's configuration causes infinite loops during content negotiation.
* @type {number}
*/
VARIANT_ALSO_NEGOTIATES: 506,
/**
* Insufficient Storage - The server cannot store the representation needed to complete the request.
* Example: A server runs out of disk space while trying to save a file upload.
* @type {number}
*/
INSUFFICIENT_STORAGE: 507,
/**
* Loop Detected - The server detected an infinite loop while processing a request.
* Example: A WebDAV request encounters a looping reference in its hierarchy.
* @type {number}
*/
LOOP_DETECTED: 508,
/**
* Not Extended - Further extensions are required for the server to fulfill the request.
* Example: A server needs additional protocol extensions to process the request.
* @type {number}
*/
NOT_EXTENDED: 510,
/**
* Network Authentication Required - The client must authenticate to gain network access.
* Example: A captive portal redirects unauthenticated users to a login page.
* @type {number}
*/
NETWORK_AUTHENTICATION_REQUIRED: 511,
});
export default httpStatusConstants;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment