Should a HttpResponse's status_code attribute be an integer or a string?

8.7k Views Asked by At

When I am creating a HttpResponse object, should I pass it a integer for the status_code attribute, or a string? I.e. HttpResponse('401 Client Error', status=401) vs. HttpResponse('401 Client Error', status="401")

The documentation is a generic descriptor, that doesn't give the type.

HttpResponse.status_code

The HTTP status code for the response.

2

There are 2 best solutions below

0
On BEST ANSWER

You should always pass an integer.

HttpResponse('401 Client Error', status=401)

As per the HTTP protocol:

The Status-Code element is a 3-digit integer result code of the attempt to understand and satisfy the request.

0
On

An integer, because the default value listed in the documentation is an integer. I.e. 200, instead of "200".

Building from Rahul Gupta's answer, an integer also because quote HTTP protocol,

The Status-Code element is a 3-digit integer result code of the attempt to understand and satisfy the request.

and because headers and metadata may be either strings or integers