2.6. HTTP Status
1XX
- Informational2XX
- Successful3XX
- Redirection4XX
- Client Error5XX
- Server Error
2.6.1. 1xx
Informational response
100
- Continue101
- Switching Protocols102
- Processing (WebDAV)103
- Early Hints
2.6.2. 2xx
Success
200
- OK201
- Created202
- Accepted203
- Non-Authoritative Information204
- No Content205
- Reset Content206
- Partial Content207
- Multi-Status (WebDAV)208
- Already Reported (WebDAV)209
- IM Used
2.6.3. 3xx
Redirection
300
- Multiple Choices301
- Moved Permanently302
- Found (Previously 'Moved temporarily')303
- See Other304
- Not Modified305
- Use Proxy306
- Switch Proxy307
- Temporary Redirect308
- Permanent Redirect
2.6.4. 4xx
Client errors
400
- Bad Request401
- Unauthorized402
- Payment Required403
- Forbidden404
- Not Found405
- Method Not Allowed406
- Not Acceptable407
- Proxy Authentication Required408
- Request Timeout409
- Conflict410
- Gone411
- Length Required412
- Precondition Failed413
- Payload Too Large414
- URI Too Long415
- Unsupported Media Type416
- Range Not Satisfiable417
- Expectation Failed418
- I'm a teapot - This code was defined in 1998 as one of the traditional IETF April Fools' jokes, in RFC 2324421
- Misdirected Request422
- Unprocessable Entity (WebDAV)423
- Locked (WebDAV)424
- Failed Dependency (WebDAV)426
- Upgrade Required428
- Precondition Required429
- Too Many Requests431
- Request Header Fields Too Large451
- Unavailable For Legal Reasons
2.6.5. 5xx
Server errors
500
- Internal Server Error501
- Not Implemented502
- Bad Gateway503
- Service Unavailable504
- Gateway Timeout505
- HTTP Version Not Supported506
- Variant Also Negotiates507
- Insufficient Storage (WebDAV)508
- Loop Detected (WebDAV)510
- Not Extended511
- Network Authentication Required
2.6.6. Built-in Match
HTTP Status
>>> status = 404
>>>
>>> match status:
... case 400: reason = 'Bad request'
... case 401 | 403 | 405: reason = 'Not allowed'
... case 404: reason = 'Not found'
... case 418: reason = "I'm a teapot"
... case _: reason = 'Unexpected status'
>>>
>>>
>>> print(reason)
Not found
2.6.7. Custom Enum
from enum import Enum
>>> from enum import Enum
>>>
>>>
>>> class HTTPStatus(Enum):
... OK = 200
... CREATED = 201
... BAD_REQUEST = 400
... NOT_FOUND = 404
... INTERNAL_ERROR = 500
>>>
>>>
>>> status = 404
>>>
>>> match HTTPStatus(status):
... case HTTPStatus.BAD_REQUEST: response = 'Bad request'
... case HTTPStatus.NOT_FOUND: response = 'Not found'
... case HTTPStatus.INTERNAL_ERROR: response = 'Internal Server Error'
... case _: response = 'Unexpected status'
>>>
>>>
>>> print(response)
Not found
2.6.8. Built-in Enum
from http import HTTPStatus
>>> from http import HTTPStatus
>>>
>>>
>>> HTTPStatus(200).name
'OK'
>>>
>>> HTTPStatus(404).name
'NOT_FOUND'
>>>
>>> HTTPStatus(500).name
'INTERNAL_SERVER_ERROR'
>>>
>>> HTTPStatus(418).name
'IM_A_TEAPOT'
Using statuses:
from http import HTTPStatus
HTTPStatus.OK
HTTPStatus.OK == 200
HTTPStatus.OK.value
HTTPStatus.OK.phrase
HTTPStatus.OK.description
list(HTTPStatus)
Most common statuses:
from http import HTTPStatus
HTTPStatus.OK # 200
HTTPStatus.CREATED # 201
HTTPStatus.MOVED_PERMANENTLY # 301
HTTPStatus.FOUND # 302
HTTPStatus.BAD_REQUEST # 400
HTTPStatus.UNAUTHORIZED # 401
HTTPStatus.FORBIDDEN # 403
HTTPStatus.METHOD_NOT_ALLOWED # 405
HTTPStatus.NOT_FOUND # 404
HTTPStatus.INTERNAL_SERVER_ERROR # 500