File: status.rst

package info (click to toggle)
python-falcon 3.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,204 kB
  • sloc: python: 28,455; makefile: 184; sh: 139; javascript: 66
file content (208 lines) | stat: -rw-r--r-- 6,404 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
.. _status:

Status Codes
============

.. contents:: :local:

Falcon provides a list of constants for common
`HTTP response status codes <http://httpstatus.es>`_.

For example:

.. code:: python

    # Override the default "200 OK" response status
    resp.status = falcon.HTTP_409

Or, using the more verbose name:

.. code:: python

    resp.status = falcon.HTTP_CONFLICT

Using these constants helps avoid typos and cuts down on the number of
string objects that must be created when preparing responses. However, starting
with Falcon version 3.0, an LRU is used to enable efficient use of
:class:`http.HTTPStatus` and bare ``int`` codes as well. Essentially, the WSGI
flavor of :attr:`resp.status <falcon.Response.status>` can now be set to
anything that :func:`~falcon.code_to_http_status` accepts:

.. code:: python

    resp.status = 201

ASGI :attr:`resp.status <falcon.asgi.Response.status>` also supports the same
broad selection of status types via :func:`~falcon.http_status_to_code`:

.. code:: python

    resp.status = HTTPStatus.CREATED

.. note::
    One notable difference between WSGI and ASGI is that the latter's HTTP
    `send <https://asgi.readthedocs.io/en/latest/specs/www.html#response-start-send-event>`__
    event defines ``status`` as an ``int`` code. Effectively, this precludes
    rendering a custom status line string or a non-standard status code that
    the ASGI app server is unaware of.

Falcon also provides a generic :class:`~.HTTPStatus` exception class. Simply
raise an instance of this class from any hook, middleware, or a responder to
stop handling the request and skip to the response handling. It takes status,
additional headers and body as input arguments.

HTTPStatus
----------

.. autoclass:: falcon.HTTPStatus
    :members:


1xx Informational
-----------------

.. code:: python

    HTTP_CONTINUE = HTTP_100
    HTTP_SWITCHING_PROTOCOLS = HTTP_101
    HTTP_PROCESSING = HTTP_102

    HTTP_100 = '100 Continue'
    HTTP_101 = '101 Switching Protocols'
    HTTP_102 = '102 Processing'

2xx Success
-----------

.. code:: python

    HTTP_OK = HTTP_200
    HTTP_CREATED = HTTP_201
    HTTP_ACCEPTED = HTTP_202
    HTTP_NON_AUTHORITATIVE_INFORMATION = HTTP_203
    HTTP_NO_CONTENT = HTTP_204
    HTTP_RESET_CONTENT = HTTP_205
    HTTP_PARTIAL_CONTENT = HTTP_206
    HTTP_MULTI_STATUS = HTTP_207
    HTTP_ALREADY_REPORTED = HTTP_208
    HTTP_IM_USED = HTTP_226

    HTTP_200 = '200 OK'
    HTTP_201 = '201 Created'
    HTTP_202 = '202 Accepted'
    HTTP_203 = '203 Non-Authoritative Information'
    HTTP_204 = '204 No Content'
    HTTP_205 = '205 Reset Content'
    HTTP_206 = '206 Partial Content'
    HTTP_207 = '207 Multi-Status'
    HTTP_208 = '208 Already Reported'
    HTTP_226 = '226 IM Used'

3xx Redirection
---------------

.. code:: python

    HTTP_MULTIPLE_CHOICES = HTTP_300
    HTTP_MOVED_PERMANENTLY = HTTP_301
    HTTP_FOUND = HTTP_302
    HTTP_SEE_OTHER = HTTP_303
    HTTP_NOT_MODIFIED = HTTP_304
    HTTP_USE_PROXY = HTTP_305
    HTTP_TEMPORARY_REDIRECT = HTTP_307
    HTTP_PERMANENT_REDIRECT = HTTP_308

    HTTP_300 = '300 Multiple Choices'
    HTTP_301 = '301 Moved Permanently'
    HTTP_302 = '302 Found'
    HTTP_303 = '303 See Other'
    HTTP_304 = '304 Not Modified'
    HTTP_305 = '305 Use Proxy'
    HTTP_307 = '307 Temporary Redirect'
    HTTP_308 = '308 Permanent Redirect'

4xx Client Error
----------------

.. code:: python

    HTTP_BAD_REQUEST = HTTP_400
    HTTP_UNAUTHORIZED = HTTP_401  # <-- Really means "unauthenticated"
    HTTP_PAYMENT_REQUIRED = HTTP_402
    HTTP_FORBIDDEN = HTTP_403  # <-- Really means "unauthorized"
    HTTP_NOT_FOUND = HTTP_404
    HTTP_METHOD_NOT_ALLOWED = HTTP_405
    HTTP_NOT_ACCEPTABLE = HTTP_406
    HTTP_PROXY_AUTHENTICATION_REQUIRED = HTTP_407
    HTTP_REQUEST_TIMEOUT = HTTP_408
    HTTP_CONFLICT = HTTP_409
    HTTP_GONE = HTTP_410
    HTTP_LENGTH_REQUIRED = HTTP_411
    HTTP_PRECONDITION_FAILED = HTTP_412
    HTTP_REQUEST_ENTITY_TOO_LARGE = HTTP_413
    HTTP_REQUEST_URI_TOO_LONG = HTTP_414
    HTTP_UNSUPPORTED_MEDIA_TYPE = HTTP_415
    HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = HTTP_416
    HTTP_EXPECTATION_FAILED = HTTP_417
    HTTP_IM_A_TEAPOT = HTTP_418
    HTTP_UNPROCESSABLE_ENTITY = HTTP_422
    HTTP_LOCKED = HTTP_423
    HTTP_FAILED_DEPENDENCY = HTTP_424
    HTTP_UPGRADE_REQUIRED = HTTP_426
    HTTP_PRECONDITION_REQUIRED = HTTP_428
    HTTP_TOO_MANY_REQUESTS = HTTP_429
    HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = HTTP_431
    HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = HTTP_451

    HTTP_400 = '400 Bad Request'
    HTTP_401 = '401 Unauthorized'  # <-- Really means "unauthenticated"
    HTTP_402 = '402 Payment Required'
    HTTP_403 = '403 Forbidden'  # <-- Really means "unauthorized"
    HTTP_404 = '404 Not Found'
    HTTP_405 = '405 Method Not Allowed'
    HTTP_406 = '406 Not Acceptable'
    HTTP_407 = '407 Proxy Authentication Required'
    HTTP_408 = '408 Request Timeout'
    HTTP_409 = '409 Conflict'
    HTTP_410 = '410 Gone'
    HTTP_411 = '411 Length Required'
    HTTP_412 = '412 Precondition Failed'
    HTTP_413 = '413 Payload Too Large'
    HTTP_414 = '414 URI Too Long'
    HTTP_415 = '415 Unsupported Media Type'
    HTTP_416 = '416 Range Not Satisfiable'
    HTTP_417 = '417 Expectation Failed'
    HTTP_418 = "418 I'm a teapot"
    HTTP_422 = "422 Unprocessable Entity"
    HTTP_423 = '423 Locked'
    HTTP_424 = '424 Failed Dependency'
    HTTP_426 = '426 Upgrade Required'
    HTTP_428 = '428 Precondition Required'
    HTTP_429 = '429 Too Many Requests'
    HTTP_431 = '431 Request Header Fields Too Large'
    HTTP_451 = '451 Unavailable For Legal Reasons'

5xx Server Error
----------------

.. code:: python

    HTTP_INTERNAL_SERVER_ERROR = HTTP_500
    HTTP_NOT_IMPLEMENTED = HTTP_501
    HTTP_BAD_GATEWAY = HTTP_502
    HTTP_SERVICE_UNAVAILABLE = HTTP_503
    HTTP_GATEWAY_TIMEOUT = HTTP_504
    HTTP_HTTP_VERSION_NOT_SUPPORTED = HTTP_505
    HTTP_INSUFFICIENT_STORAGE = HTTP_507
    HTTP_LOOP_DETECTED = HTTP_508
    HTTP_NETWORK_AUTHENTICATION_REQUIRED = HTTP_511

    HTTP_500 = '500 Internal Server Error'
    HTTP_501 = '501 Not Implemented'
    HTTP_502 = '502 Bad Gateway'
    HTTP_503 = '503 Service Unavailable'
    HTTP_504 = '504 Gateway Timeout'
    HTTP_505 = '505 HTTP Version Not Supported'
    HTTP_507 = '507 Insufficient Storage'
    HTTP_508 = '508 Loop Detected'
    HTTP_511 = '511 Network Authentication Required'