UNPKG

7.59 kBMarkdownView Raw
1# http-status-codes
2
3Constants enumerating the HTTP status codes. Based on the [Java Apache HttpStatus API](http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpStatus.html).
4
5All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), RFC2518 (WebDAV), RFC6585 (Additional HTTP Status Codes), and RFC7538 (Permanent Redirect) are supported.
6
7TypeScript or JavaScript. Completely library agnostic. No dependencies.
8
9## Installation
10
11```console
12npm install http-status-codes --save
13```
14
15## Usage (express 4.x)
16
17```typescript
18import {
19 ReasonPhrases,
20 StatusCodes,
21 getReasonPhrase,
22 getStatusCode,
23} from 'http-status-codes';
24
25response
26 .status(StatusCodes.OK)
27 .send(ReasonPhrases.OK);
28
29response
30 .status(StatusCodes.INTERNAL_SERVER_ERROR)
31 .send({
32 error: getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR)
33 });
34
35response
36 .status(getStatusCode('Internal Server Error'))
37 .send({
38 error: 'Internal Server Error'
39 });
40```
41
42## Codes
43
44| Code | Constant | Reason Phrase |
45| ---- | ------------------------------- | ------------------------------- |
46| 100 | CONTINUE | Continue |
47| 101 | SWITCHING_PROTOCOLS | Switching Protocols |
48| 102 | PROCESSING | Processing |
49| 103 | EARLY_HINTS | Early Hints |
50| 200 | OK | OK |
51| 201 | CREATED | Created |
52| 202 | ACCEPTED | Accepted |
53| 203 | NON_AUTHORITATIVE_INFORMATION | Non Authoritative Information |
54| 204 | NO_CONTENT | No Content |
55| 205 | RESET_CONTENT | Reset Content |
56| 206 | PARTIAL_CONTENT | Partial Content |
57| 207 | MULTI_STATUS | Multi-Status |
58| 300 | MULTIPLE_CHOICES | Multiple Choices |
59| 301 | MOVED_PERMANENTLY | Moved Permanently |
60| 302 | MOVED_TEMPORARILY | Moved Temporarily |
61| 303 | SEE_OTHER | See Other |
62| 304 | NOT_MODIFIED | Not Modified |
63| 305 | USE_PROXY | Use Proxy |
64| 307 | TEMPORARY_REDIRECT | Temporary Redirect |
65| 308 | PERMANENT_REDIRECT | Permanent Redirect |
66| 400 | BAD_REQUEST | Bad Request |
67| 401 | UNAUTHORIZED | Unauthorized |
68| 402 | PAYMENT_REQUIRED | Payment Required |
69| 403 | FORBIDDEN | Forbidden |
70| 404 | NOT_FOUND | Not Found |
71| 405 | METHOD_NOT_ALLOWED | Method Not Allowed |
72| 406 | NOT_ACCEPTABLE | Not Acceptable |
73| 407 | PROXY_AUTHENTICATION_REQUIRED | Proxy Authentication Required |
74| 408 | REQUEST_TIMEOUT | Request Timeout |
75| 409 | CONFLICT | Conflict |
76| 410 | GONE | Gone |
77| 411 | LENGTH_REQUIRED | Length Required |
78| 412 | PRECONDITION_FAILED | Precondition Failed |
79| 413 | REQUEST_TOO_LONG | Request Entity Too Large |
80| 414 | REQUEST_URI_TOO_LONG | Request-URI Too Long |
81| 415 | UNSUPPORTED_MEDIA_TYPE | Unsupported Media Type |
82| 416 | REQUESTED_RANGE_NOT_SATISFIABLE | Requested Range Not Satisfiable |
83| 417 | EXPECTATION_FAILED | Expectation Failed |
84| 418 | IM_A_TEAPOT | I'm a teapot |
85| 419 | INSUFFICIENT_SPACE_ON_RESOURCE | Insufficient Space on Resource |
86| 420 | METHOD_FAILURE | Method Failure |
87| 421 | MISDIRECTED_REQUEST | Misdirected Request |
88| 422 | UNPROCESSABLE_ENTITY | Unprocessable Entity |
89| 423 | LOCKED | Locked |
90| 424 | FAILED_DEPENDENCY | Failed Dependency |
91| 426 | UPGRADE_REQUIRED | Upgrade Required |
92| 428 | PRECONDITION_REQUIRED | Precondition Required |
93| 429 | TOO_MANY_REQUESTS | Too Many Requests |
94| 431 | REQUEST_HEADER_FIELDS_TOO_LARGE | Request Header Fields Too Large |
95| 451 | UNAVAILABLE_FOR_LEGAL_REASONS | Unavailable For Legal Reasons |
96| 500 | INTERNAL_SERVER_ERROR | Internal Server Error |
97| 501 | NOT_IMPLEMENTED | Not Implemented |
98| 502 | BAD_GATEWAY | Bad Gateway |
99| 503 | SERVICE_UNAVAILABLE | Service Unavailable |
100| 504 | GATEWAY_TIMEOUT | Gateway Timeout |
101| 505 | HTTP_VERSION_NOT_SUPPORTED | HTTP Version Not Supported |
102| 507 | INSUFFICIENT_STORAGE | Insufficient Storage |
103| 511 | NETWORK_AUTHENTICATION_REQUIRED | Network Authentication Required |
104
105## Migrating from v1.x.x
106
107http-status-codes v2 is mostly backwards compatible with v1. There is a single breaking change and two recommended changes.
108
109#### [Breaking Change] 'Server Error'
110
111The reason phrase for the status code `500` has been changed from `"Server Error"` to `"Internal Server Error"`. This is the correct phrase according to RFC7231. If you are migrating from v1, and have code that relies on the result of `getStatusText(500)` or `getReasonPhrase('Server Error')`, then this could affect you.
112
113#### [Non-breaking change] getStatusText renamed getReasonPhrase
114
115The function `getStatusText` has been renamed to `getReasonPhrase`. The old function is still available, but may be deprecated in a future version. To fix this simply rename instances of `getStatusText()` to `getReasonPhrase()`. The function is otherwise the same as it was before.
116
117#### [Non-breaking change] StatusCodes
118
119In http-status-codes v1, Status Codes were exported directly from the top-level module. i.e. `HttpStatus.OK`. In v2 all Status Codes live under an object called `StatusCodes`. i.e. `HttpStatus.StatusCodes.OK`. We made this change to cater to TypeScript users who prefer a dedicated value with an enum type. The previous values are still exported, but we won't continue to update them. Please migrate if you're using the old-style imports.
120
121## Proposing a new status code
122
123If you'd like to propose a new status code, feel free to update "codes.json" with the necessary
124information and open a pull request. There is no need to modify source code or even this README.
125This is done automatically by `npm run update-codes`.
126
127In general, we try to include only codes that have an official RFC and have been approved, however
128exceptions can be made if the code is already in widespread use in the wild.
129
130## Steps to build and publish
131
132```shell
133npm run update-codes
134npm run test
135npm run build
136npm version [major | minor | patch]
137npm publish
138```
139
140After releasing, please add release notes via GitHub Releases.