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

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


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.


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

import requestsimport jsonheaderInfo = {'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.textprint r.status_code


You Can pass {

  'Content-Type': 'application/json;charset=UTF-8'}

it worked