This error occurs when you try to get the content from a RESTful API request with the requests library. The read()
method does not belong to the response object, it is a File method.
You can solve this error by using text to get the string representation of the response or content to get a sequence of bytes representing the response. For example,
import requests response = requests.get("https://www.google.com") print(response.text)
This tutorial will go through the error in detail and how to solve it with code examples.
AttributeError: ‘Response’ object has no attribute ‘read’
AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The read()
method belongs to the File data type and returns the specified number of bytes from the file. If we want to get the content of the response we need to use text.
Example
Let’s look at an example of a GET request. We will use the requests library to perform the request:
import requests resp = requests.get("https://google.com") content = resp.read()
Let’s run the code to see what happens:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [1], in <cell line: 5>() 1 import requests 3 resp = requests.get("https://google.com") ----> 5 content = resp.read() AttributeError: 'Response' object has no attribute 'read'
The error occurs because requests.get()
returns a requests.Response
object not a File. The requests.Response
object contains the server’s response to the HTTP request.
Solution
We can solve the error by replacing the read()
method call with text, which returns the content of the response in Unicode. Let’s look at the revised code:
import requests resp = requests.get("https://google.com") content = resp.text print(content)
Let’s run the code to get the result:
<!DOCTYPE html><html lang="en" dir="ltr"><head><style nonce="4p3rLOiPq10zoKyKEIRhsw"> a, a:link, a:visited, a:active, a:hover { color: #1a73e8; text-decoration: none; }
The output above shows the first lines of the HTTP GET response.
In Python 2, Response.content is equivalent to Response.text. As of Python 3 Response.content
returns the content of the response in bytes and Response.text
alone returns the content as a string. If we want to use the content in Python 3 we will get bytes, which we can convert to a string using str()
. Let’s look at the revised code:
import requests resp = requests.get("https://google.com") content = resp.content print(str(content))
Let’s run the code to see the result:
b'<!DOCTYPE html><html lang="en" dir="ltr"><head><style nonce="W-5zkPQzBWuTAZSAhyHXew">\na, a:link, a:visited, a:active, a:hover {\n color: #1a73e8;\n text-decoration: none;\n}\nbody
The output above shows the first lines of the HTTP GET response in bytes converted to a string.
Summary
Congratulations on reading to the end of this tutorial! The AttributeError: ‘Response’ object has no attribute ‘read’ occurs when you call the read()
method on a Response object. Ensure that you use the text method when you want to access the contents Response object as a string.
For further reading on AttributeErrors, go to the article:
How to Solve Python AttributeError: ‘Response’ object has no attribute ‘get’
To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.