Why do I get error AttributeError: 'Response' object has no attribute 'get' in Python2.7?

Dipanwita Das picture Dipanwita Das · Sep 4, 2018 · Viewed 22.6k times · Source

I am getting error AttributeError: 'Response' object has no attribute 'get' for the below code I have written

def convert_json(self,bucket,userid,imgfilename,field,i):

    bucketName = bucket
    link = "users_"+str(userid)+'/'+imgfilename
    c = S3Connection(self.AWS_ACCESS_KEY_ID,self.AWS_ACCESS_KEY_SECRET)
    p = c.generate_url(expires_in=long(7200),method='GET',bucket=bucketName,key=link,query_auth=True,force_http=False)  
    post_url = "http://someurl"
    wrapper = {"filename":p}
    try:
        response = requests.post(post_url, json=wrapper)
        print response
        if response.status_code == 200:
            text = response.get('description', [])
        else:
            text = []
    except Exception:
        if response.status_code == 200:
            text = response.get('description', [])
        else:
            text = []
    return text

Answer

Preston picture Preston · Oct 29, 2019

The object isn't a dictionary, so you can't use get. You'll likely find what you need with either:

  • r.status_code
  • r.content
  • r.text
  • r.json()

To cite the example given on the requests page:

>>> import requests
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'disk_usage': 368627, u'private_gists': 484, ...}