49

I would like to be able to enter a server response code and have Requests tell me what the code means. For example, code 200 --> ok

I found a link to the source code which shows the dictionary structure of the codes and descriptions. I see that Requests will return a response code for a given description:

print requests.codes.processing  # returns 102
print requests.codes.ok          # returns 200
print requests.codes.not_found   # returns 404

But not the other way around:

print requests.codes[200]        # returns None
print requests.codes.viewkeys()  # returns dict_keys([])
print requests.codes.keys()      # returns []

I thought this would be a routine task, but cannot seem to find an answer to this in online searching, or in the documentation.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Roberto
  • 2,054
  • 4
  • 31
  • 46

5 Answers5

63

Alternatively, in case of Python 2.x, you can use httplib.responses:

>>> import httplib
>>> httplib.responses[200]
'OK'
>>> httplib.responses[404]
'Not Found'

In Python 3.x, use http module:

In [1]: from http.client import responses

In [2]: responses[200]
Out[2]: 'OK'

In [3]: responses[404]
Out[3]: 'Not Found'
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    I preferred to stay within `Requests`, but this is a useful alternative; thanks for sharing. – Roberto Jul 13 '14 at 11:07
  • 1
    @Roberto `httplib` is a stock module, so there's nothing encumbered here. – ivan_pozdeev Oct 19 '16 at 10:22
  • httplib is a Python 2.7 thing; from 3.5 there's `http.HTTPStatus`. Where is it in 3.4? – user508402 Feb 21 '17 at 13:37
  • 1
    @user508402 did you mean to use `from http.client import responses` for the mapping between status code and messages? (I've also updated the answer and included the 3.x version) Thanks. – alecxe Feb 21 '17 at 14:39
  • Ah yes, there they are. With that list it is easy to make my own duck typed HTTPStatus. Thanks. – user508402 Feb 21 '17 at 19:19
28

One possibility:

>>> import requests
>>> requests.status_codes._codes[200]
('ok', 'okay', 'all_ok', 'all_okay', 'all_good', '\\o/', '\xe2\x9c\x93')

The first value in the tuple is used as the conventional code key.

shux
  • 335
  • 2
  • 5
  • 9
    `requests.status_codes._codes` is an implementation detail you shouldn't be touching. Note the don't-touch-me underscore on `_codes`. Such implementation details are subject to change or removal without notice. – user2357112 Aug 16 '21 at 07:06
16

I had the same problem before and found the answer in this question

Basically:

  • responsedata.status_code - gives you the integer status code
  • responsedata.reason - gives the text/string representation of the status code
Frightera
  • 4,773
  • 2
  • 13
  • 28
nuwang
  • 161
  • 1
  • 4
  • This has to be comment rather than answering to the questions – Bruce Feb 05 '21 at 08:44
  • 2
    I don't follow what you mean. The current answer with most votes is to use `httplib`. But the `requests` library already provides (within itself) the functionality to decipher the meaning (or string description) of the response code. You don't have to go elsewhere for this. – nuwang Feb 17 '21 at 07:22
  • I don't think reason exists in earlier versions of responses. – Jonathan Roberts Nov 04 '21 at 12:32
  • @JonathanRoberts how early are we talking here? It was available in requests version 2.0 (committed september 2013) https://github.com/psf/requests/blob/4f463e897afb15db253994829af24928cf4f4535/requests/models.py#L518 – Philip Couling Oct 31 '22 at 22:13
  • As far as I know, the `Response.reason` is provided server-side. This might be useful or dangerous depending on your intended use case. – Philip Couling Oct 31 '22 at 22:14
1

With Python 3.x this will work

>>> from http import HTTPStatus
>>> HTTPStatus(200).phrase
'OK'
mat1010
  • 756
  • 1
  • 9
  • 17
-1

requests.status_codes.codes.OK

works nicely and makes it more readable in my application code

Notice that in source code: the requests.status_codes.codes is of type LookupDict which overrides method getitem

You could see all the supported keys with - dir(requests.status_codes.codes)

When using in combination with FLASK:

i like use following enum from flask-api plugin from flask_api import status where i get more descriptive version of HTTP status codes as in -

status.HTTP_200_OK

ofbyone
  • 15
  • 2
  • 1
    You've described how to get the numerical value from the name. The question was about how to get the text description from the numerical value - the reverse operation. – Ian Goldby Jan 20 '20 at 11:31