close

[Solved] AttributeError: ‘str’ object has no attribute ‘items’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: ‘str’ object has no attribute ‘items’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: ‘str’ object has no attribute ‘items’ Error Occurs?

Today I get the following error AttributeError: ‘str’ object has no attribute ‘items’ in python.

How To Solve AttributeError: ‘str’ object has no attribute ‘items’ Error ?

  1. How To Solve AttributeError: 'str' object has no attribute 'items' Error ?

    To Solve AttributeError: 'str' object has no attribute 'items' Error You are passing in a stringheaders can't ever be a JSON encoded string, it is always a Python dictionary.

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

    To Solve AttributeError: 'str' object has no attribute 'items' Error You are passing in a stringheaders can't ever be a JSON encoded string, it is always a Python dictionary.

Solution 1

You are passing in a stringheaders 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.

Solution 2

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read