Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'str' object has no attribute 'items'

Tags:

In the following code:

#!/usr/local/bin/python import json  APPLICATION_NAME = 'cc9226315643df89-36bf02429075329d0ba36748360d050c'  HEADERS1 = json.dumps(dict(Destination = u"/api/af/latest/applications/%s/rulesets" % (APPLICATION_NAME))) print "Headers1 is %s" % (HEADERS1) HEADERS2 = {'Destination': '/api/af/latest/applications/%s/rulesets' % (APPLICATION_NAME)} print "Headers2 is %s" % (HEADERS2) 

I get the following output:

Headers1 is {"Destination": "/api/af/latest/applications/cc9226315643df89-36bf02429075329d0ba36748360d050c/rulesets"} Headers2 is {'Destination': '/api/af/latest/applications/cc9226315643df89-36bf02429075329d0ba36748360d050c/rulesets'} 

but when I try to use either HEADER1 or HEADER2 in a REST call using requests(), I get very different results:

SERVER_URL = 'http://1.1.33.109:8087%s' % (APP_PATH) REQ_DATA = None print "Headers are: ", HEADERS print "SERVER_URL is: ", SERVER_URL print "Request Data is:", REQ_DATA print ""  RESPONSE = requests.request(     'MOVE',      SERVER_URL,      auth = ('admin', 'admin'),      verify = False,      data = REQ_DATA,     headers = HEADERS1 )     #<-- If I use HEADER1 it breaks, if I use HEADER2 it works print "Move Ruleset back to the Application RESULT: %s\n" % (RESPONSE) 

I get the following with HEADER1:

Traceback (most recent call last):    File "./myrest.py", line 234, in <module>      headers = HEADERS1 )    File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 44, in request      return session.request(method=method, url=url, **kwargs)    File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 324, in request      prep = req.prepare()    File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 223, in prepare      p.prepare_headers(self.headers)    File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 340, in prepare_headers      headers = dict((name.encode('ascii'), value) for name, value in headers.items()) AttributeError: 'str' object has no attribute 'items' 

If I use HEADER2 it executes cleanly:

Move Ruleset back to the Application RESULT: Response [200]

Can anyone explain what the differences are?

like image 212
snrkl Avatar asked Sep 18 '13 08:09

snrkl


People also ask

How do I fix AttributeError str object has no attribute?

The Python "AttributeError: 'str' object has no attribute" occurs when we try to access an attribute that doesn't exist on string objects. To solve the error, make sure the value is of the expected type before accessing the attribute.

How do I fix AttributeError str object has no attribute append?

The Python "AttributeError: 'str' object has no attribute 'append'" occurs when we try to call the append() method on a string (e.g. a list element at specific index). To solve the error, call the append method on the list or use the addition (+) operator if concatenating strings.

How do you pop a string to an object in Python?

The "AttributeError: 'str' object has no attribute 'pop'" occurs when we try to call the pop() method on a string instead of a list. To solve the error, call the pop() method on a list or use the rsplit() method if you need to remove the last word from a string.

What does object has no attribute mean in Python?

It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.


2 Answers

You are passing in a string; headers can't ever be a JSON encoded string, it is always a Python dictionary.

The print results are deceptive; JSON encoded objects look a lot like Python dictionary representations but they are far from the same thing.

The requests API clearly states that headers must be a dictionary:

  • headers – (optional) Dictionary of HTTP Headers to send with the Request.

JSON data is something you'd send as content to another server, not something you'd use to communicate with a Python API.

like image 156
Martijn Pieters Avatar answered Oct 20 '22 14:10

Martijn Pieters


I had this issue and I needed to make the header with a content type and pass in a data element as json.

import requests import json  headerInfo = {'content-type': 'application/json' } payload = {'text': 'okay!!!', 'auth_token': 'aasdasdasdasd'} jLoad = json.dumps(payload)  r = requests.post('http://example.com:3030/widgets/init', headers=headerInfo, data=jLoad) print r.text print r.status_code 
like image 41
ddtraveller Avatar answered Oct 20 '22 15:10

ddtraveller