Python Requests - response.reason

The response.reason attribute from the Python request package accepts a textual description of the specified HTTP status code. For example, this service might associate a 404 status code with its HTTP message, Not Found. Stated differently, you can use the response object from the request module to identify the message returned by the request. The response.reason property provides text corresponding to the HTTP status code.

  1. The status code 200 displays the OK message.
  2. The status code 404 displays the Not Found message.
  3. The status code 500 displays the Internal Server Error message.
Python Requests - response.reason

When using Python's requests library, we typically make HTTP requests to a specific URI. This request returns a response object with various properties and methods for interacting with the data received from the server. One of these properties is response.reason. According to the status code of the server, we get a textual representation. Let us understand by taking a practical example.

Note: First, make sure you have Python and the requests library installed on your system.
After that, create a Python script, i.e., request.py, with the following code:

Example

Now, save the above code as request.py and run the below command in the CMD or VS code terminal.

Output

<Response [200]>
OK
<Response [500]>
Internal Server Error

Explanation

First, we have imported the 'requests' library in the above code to make HTTP GET requests to two URLs. Then, based on the responses from each URL request, the responses will be displayed, along with the status code and the reason.

Status Code and response.reason Attribute:

When the server receives an HTTP request, it responds with an appropriate HTTP status code and description. The status code indicates the conditions of the request; It can be a success, a redirect, a client-side error, or a server error. We can get this information easily by accessing the response.reason attribute. To understand better, let's take another example.

Example:

Output

The request is Successful! Reason: OK

Explanation

The above code sends a request to the website 'https://seowebfix.com' and checks its status. If the status code is 200, it gives the output: Success and the reason OK. If the status code is 404 or 500, it gives the output: Not Found or Server Error with the reason.

For example, if we provide an incorrect web url like 'https://seowebfix.com/incorrect' instead of https://seowebfix.com/, a message will be displayed: 404 Page Not Found message. Something like the below message:

Code

Output

The request has failed! Status code: 404, Reason: Not Found

Explanation

In the above code, we send a request to 'https://seowebfix.com/vivek'. It checks if the response status code is 200 (success) and prints a success message with the reason. If the status code is not 200, it prints a failure message along with the status code and reason.

Conclusion

Using the response.reason attribute from Python's Requests library to retrieve the reason for an HTTP response status code becomes easy. This feature increases code readability and makes error handling feasible by providing descriptive explanations for the response status. Using response.reason; one can easily identify the outcome of HTTP requests. In addition, it facilitates efficient troubleshooting and provides more accurate information to users.