Status Codes

    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.

    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 and RFC 6585.

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

    Successful - 2xx

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

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

    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_412_PRECONDITION_FAILED
    13. HTTP_414_REQUEST_URI_TOO_LONG
    14. HTTP_415_UNSUPPORTED_MEDIA_TYPE
    15. HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE
    16. HTTP_417_EXPECTATION_FAILED
    17. HTTP_422_UNPROCESSABLE_ENTITY
    18. HTTP_423_LOCKED
    19. HTTP_424_FAILED_DEPENDENCY
    20. HTTP_426_UPGRADE_REQUIRED
    21. HTTP_428_PRECONDITION_REQUIRED
    22. HTTP_429_TOO_MANY_REQUESTS
    23. HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE
    24. HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS

    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.

    Helper functions

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