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.

5 Answers 5

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'
5
  • 1
    I preferred to stay within Requests, but this is a useful alternative; thanks for sharing.
    – Roberto
    Jul 13, 2014 at 11:07
  • 1
    @Roberto httplib is a stock module, so there's nothing encumbered here. Oct 19, 2016 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, 2017 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, 2017 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, 2017 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.

1
  • 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. Aug 16, 2021 at 7: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
5
  • This has to be comment rather than answering to the questions
    – Bruce
    Feb 5, 2021 at 8: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, 2021 at 7:22
  • I don't think reason exists in earlier versions of responses. Nov 4, 2021 at 12:32
  • @JonathanRoberts how early are we talking here? It was available in requests version 2.0 (committed september 2013) github.com/psf/requests/blob/… Oct 31, 2022 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. Oct 31, 2022 at 22:14
1

With Python 3.x this will work

>>> from http import HTTPStatus
>>> HTTPStatus(200).phrase
'OK'
-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

1
  • 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, 2020 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.