status.py

Status Codes

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.

RFC 2324, Hyper Text Coffee Pot Control Protocol

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.

  1. from rest_framework import status
  2. from rest_framework.response import Response
  3. def empty_view(self):
  4. content = {'please move along': 'nothing to see here'}
  5. return Response(content, status=status.HTTP_404_NOT_FOUND)

The full set of HTTP status codes included in the status module is listed below.

The module also includes a set of helper functions for testing if a status code is in a given range.

  1. from rest_framework import status
  2. from rest_framework.test import APITestCase
  3. class ExampleTestCase(APITestCase):
  4. def test_url_root(self):
  5. url = reverse('index')
  6. response = self.client.get(url)
  7. self.assertTrue(status.is_success(response.status_code))

For more information on proper usage of HTTP status codes see RFC 2616and RFC 6585.

Informational - 1xx

This class of status code indicates a provisional response. There are no 1xx status codes used in REST framework by default.

  1. HTTP_100_CONTINUE
  2. HTTP_101_SWITCHING_PROTOCOLS

Successful - 2xx

This class of status code indicates that the client's request was successfully received, understood, and accepted.

  1. HTTP_200_OK
  2. HTTP_201_CREATED
  3. HTTP_202_ACCEPTED
  4. HTTP_203_NON_AUTHORITATIVE_INFORMATION
  5. HTTP_204_NO_CONTENT
  6. HTTP_205_RESET_CONTENT
  7. HTTP_206_PARTIAL_CONTENT
  8. HTTP_207_MULTI_STATUS
  9. HTTP_208_ALREADY_REPORTED
  10. HTTP_226_IM_USED

Redirection - 3xx

This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request.

  1. HTTP_300_MULTIPLE_CHOICES
  2. HTTP_301_MOVED_PERMANENTLY
  3. HTTP_302_FOUND
  4. HTTP_303_SEE_OTHER
  5. HTTP_304_NOT_MODIFIED
  6. HTTP_305_USE_PROXY
  7. HTTP_306_RESERVED
  8. HTTP_307_TEMPORARY_REDIRECT
  9. HTTP_308_PERMANENT_REDIRECT

Client Error - 4xx

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.

  1. HTTP_400_BAD_REQUEST
  2. HTTP_401_UNAUTHORIZED
  3. HTTP_402_PAYMENT_REQUIRED
  4. HTTP_403_FORBIDDEN
  5. HTTP_404_NOT_FOUND
  6. HTTP_405_METHOD_NOT_ALLOWED
  7. HTTP_406_NOT_ACCEPTABLE
  8. HTTP_407_PROXY_AUTHENTICATION_REQUIRED
  9. HTTP_408_REQUEST_TIMEOUT
  10. HTTP_409_CONFLICT
  11. HTTP_410_GONE
  12. HTTP_411_LENGTH_REQUIRED
  13. HTTP_412_PRECONDITION_FAILED
  14. HTTP_413_REQUEST_ENTITY_TOO_LARGE
  15. HTTP_414_REQUEST_URI_TOO_LONG
  16. HTTP_415_UNSUPPORTED_MEDIA_TYPE
  17. HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE
  18. HTTP_417_EXPECTATION_FAILED
  19. HTTP_422_UNPROCESSABLE_ENTITY
  20. HTTP_423_LOCKED
  21. HTTP_424_FAILED_DEPENDENCY
  22. HTTP_426_UPGRADE_REQUIRED
  23. HTTP_428_PRECONDITION_REQUIRED
  24. HTTP_429_TOO_MANY_REQUESTS
  25. HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE
  26. HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS

Server Error - 5xx

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.

  1. HTTP_500_INTERNAL_SERVER_ERROR
  2. HTTP_501_NOT_IMPLEMENTED
  3. HTTP_502_BAD_GATEWAY
  4. HTTP_503_SERVICE_UNAVAILABLE
  5. HTTP_504_GATEWAY_TIMEOUT
  6. HTTP_505_HTTP_VERSION_NOT_SUPPORTED
  7. HTTP_506_VARIANT_ALSO_NEGOTIATES
  8. HTTP_507_INSUFFICIENT_STORAGE
  9. HTTP_508_LOOP_DETECTED
  10. HTTP_509_BANDWIDTH_LIMIT_EXCEEDED
  11. HTTP_510_NOT_EXTENDED
  12. HTTP_511_NETWORK_AUTHENTICATION_REQUIRED

Helper functions

The following helper functions are available for identifying the category of the response code.

  1. is_informational() # 1xx
  2. is_success() # 2xx
  3. is_redirect() # 3xx
  4. is_client_error() # 4xx
  5. is_server_error() # 5xx