This error occurs when you try to use the Dictionary method get() to access values from a Response object. You can solve this error by converting the Response object to a JSON object. Once you have a JSON object, you can access values using the get() method.

This tutorial will go through how to solve the error with code examples.


AttributeError: ‘Response’ object has no attribute ‘get’

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 get() method belongs to the Dictionary data type and returns the item’s value with the specified key. If we want to get the content of the response in a dictionary format, we can use response.json().

Example

Let’s look at an example of executing a GET call to a web service. HTTPbin allows test requests and responds. We will try to get a value from the response using get().

import requests

resp = requests.get("https://httpbin.org/get")

print("Origin is:\n")
print(resp.get("origin"))

Let’s try to run the code to see what happens:

Origin is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [9], in <cell line: 7>()
      4 resp.raise_for_status
      6 print("Origin is:\n")
----> 7 print(resp.get("origin"))

AttributeError: 'Response' object has no attribute 'get'

The error occurs because the resp variable is a Response object:

print(type(resp))
<class 'requests.models.Response'>

Solution

We can solve this error by converting the Response object to a JSON object.

The requests module provides a built-in JSON decoder to deal with JSON data.

The response.json() function returns a JSON response if the JSON decode works properly and raises an exception if the JSON decoder fails.

We must check response.raise_for_status or response.status_code before calling response.json(), because a successful response.json() call does not mean the request was successful.

Let’s look at the revised code:

import requests
resp = requests.get("https://httpbin.org/get")

resp.raise_for_status
jsonResponse = resp.json()
print("Origin is:\n")
print(jsonResponse.get("origin"))

Let’s run the code to get the result:

Origin is:

90.206.95.191

We successfully retrieved the value under the key “origin” from the JSON response. We can verify that resp is a requests.models.Response object and jsonResponse is a Dictionary using the built-in type() function.

print(type(resp))
print(type(jsonResponse))
<class 'requests.models.Response'>
<class 'dict'>

We can iterate through the key-value pairs in the JSON response using items():

print("Print each key-value pair from JSON response\n")
for key, value in jsonResponse.items():
    print(f'{key} : {value}')
Print each key-value pair from JSON response

args : {}
headers : {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.27.1', 'X-Amzn-Trace-Id': 'Root=1-627eb6a2-4a33a78f5c70c81003d0b694'}
origin : 90.206.95.191
url : https://httpbin.org/get

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: ‘Response’ object has no attribute ‘get’ occurs when you call the get() method on a Response object. Ensure that you convert the Response to a dictionary using response.json().

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!