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

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

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.