Debian Codegraph @

docs/api-guide/status-codes.md

raw
1 ---
2 source:
3 - status.py
4 ---
5
6 # Status Codes
7
8 > 418 I'm a teapot - Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout.
9 >
10 > — [RFC 2324][rfc2324], Hyper Text Coffee Pot Control Protocol
11
12 Using bare status codes in your responses isn't recommended. REST framework includes a set of named constants that you can use to make your code more obvious and readable.
13
14 from rest_framework import status
15 from rest_framework.response import Response
16
17 def empty_view(self):
18 content = {'please move along': 'nothing to see here'}
19 return Response(content, status=status.HTTP_404_NOT_FOUND)
20
21 The full set of HTTP status codes included in the `status` module is listed below.
22
23 The module also includes a set of helper functions for testing if a status code is in a given range.
24
25 from rest_framework import status
26 from rest_framework.test import APITestCase
27
28 class ExampleTestCase(APITestCase):
29 def test_url_root(self):
30 url = reverse('index')
31 response = self.client.get(url)
32 self.assertTrue(status.is_success(response.status_code))
33
34
35 For more information on proper usage of HTTP status codes see [RFC 2616][rfc2616]
36 and [RFC 6585][rfc6585].
37
38 ## Informational - 1xx
39
40 This class of status code indicates a provisional response. There are no 1xx status codes used in REST framework by default.
41
42 HTTP_100_CONTINUE
43 HTTP_101_SWITCHING_PROTOCOLS
44 HTTP_102_PROCESSING
45 HTTP_103_EARLY_HINTS
46
47 ## Successful - 2xx
48
49 This class of status code indicates that the client's request was successfully received, understood, and accepted.
50
51 HTTP_200_OK
52 HTTP_201_CREATED
53 HTTP_202_ACCEPTED
54 HTTP_203_NON_AUTHORITATIVE_INFORMATION
55 HTTP_204_NO_CONTENT
56 HTTP_205_RESET_CONTENT
57 HTTP_206_PARTIAL_CONTENT
58 HTTP_207_MULTI_STATUS
59 HTTP_208_ALREADY_REPORTED
60 HTTP_226_IM_USED
61
62 ## Redirection - 3xx
63
64 This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request.
65
66 HTTP_300_MULTIPLE_CHOICES
67 HTTP_301_MOVED_PERMANENTLY
68 HTTP_302_FOUND
69 HTTP_303_SEE_OTHER
70 HTTP_304_NOT_MODIFIED
71 HTTP_305_USE_PROXY
72 HTTP_306_RESERVED
73 HTTP_307_TEMPORARY_REDIRECT
74 HTTP_308_PERMANENT_REDIRECT
75
76 ## Client Error - 4xx
77
78 The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.
79
80 HTTP_400_BAD_REQUEST
81 HTTP_401_UNAUTHORIZED
82 HTTP_402_PAYMENT_REQUIRED
83 HTTP_403_FORBIDDEN
84 HTTP_404_NOT_FOUND
85 HTTP_405_METHOD_NOT_ALLOWED
86 HTTP_406_NOT_ACCEPTABLE
87 HTTP_407_PROXY_AUTHENTICATION_REQUIRED
88 HTTP_408_REQUEST_TIMEOUT
89 HTTP_409_CONFLICT
90 HTTP_410_GONE
91 HTTP_411_LENGTH_REQUIRED
92 HTTP_412_PRECONDITION_FAILED
93 HTTP_413_REQUEST_ENTITY_TOO_LARGE
94 HTTP_414_REQUEST_URI_TOO_LONG
95 HTTP_415_UNSUPPORTED_MEDIA_TYPE
96 HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE
97 HTTP_417_EXPECTATION_FAILED
98 HTTP_421_MISDIRECTED_REQUEST
99 HTTP_422_UNPROCESSABLE_ENTITY
100 HTTP_423_LOCKED
101 HTTP_424_FAILED_DEPENDENCY
102 HTTP_425_TOO_EARLY
103 HTTP_426_UPGRADE_REQUIRED
104 HTTP_428_PRECONDITION_REQUIRED
105 HTTP_429_TOO_MANY_REQUESTS
106 HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE
107 HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS
108
109 ## Server Error - 5xx
110
111 Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has erred or is incapable of performing the request. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.
112
113 HTTP_500_INTERNAL_SERVER_ERROR
114 HTTP_501_NOT_IMPLEMENTED
115 HTTP_502_BAD_GATEWAY
116 HTTP_503_SERVICE_UNAVAILABLE
117 HTTP_504_GATEWAY_TIMEOUT
118 HTTP_505_HTTP_VERSION_NOT_SUPPORTED
119 HTTP_506_VARIANT_ALSO_NEGOTIATES
120 HTTP_507_INSUFFICIENT_STORAGE
121 HTTP_508_LOOP_DETECTED
122 HTTP_509_BANDWIDTH_LIMIT_EXCEEDED
123 HTTP_510_NOT_EXTENDED
124 HTTP_511_NETWORK_AUTHENTICATION_REQUIRED
125
126 ## Helper functions
127
128 The following helper functions are available for identifying the category of the response code.
129
130 is_informational() # 1xx
131 is_success() # 2xx
132 is_redirect() # 3xx
133 is_client_error() # 4xx
134 is_server_error() # 5xx
135
136 [rfc2324]: https://www.ietf.org/rfc/rfc2324.txt
137 [rfc2616]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
138 [rfc6585]: https://tools.ietf.org/html/rfc6585