Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 690
noɥʇʎԀʎzɐɹƆ Avatar asked Feb 10 '23 21:02

noɥʇʎԀʎzɐɹƆ


2 Answers

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.

like image 140
Rahul Gupta Avatar answered Feb 12 '23 11:02

Rahul Gupta


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

like image 26
noɥʇʎԀʎzɐɹƆ Avatar answered Feb 12 '23 10:02

noɥʇʎԀʎzɐɹƆ