Requests: AttributeError: 'set' object has no attribute 'items'

Created on 9 Jan 2017  路  5Comments  路  Source: psf/requests

fresh pip install requests. version 2.12.4. This works in postman.

import requests
url = 'https://api.spotify.com/v1/audio-analysis/4JpKVNYnVcJ8tuMKjAj50A'
headers = {'Authorization', 'Bearer TOKEN-HERE'}
r = requests.get(url, headers=headers)
AttributeError                            Traceback (most recent call last)
<ipython-input-5-0b5138b50602> in <module>()
      1 url = 'https://api.spotify.com/v1/audio-analysis/4JpKVNYnVcJ8tuMKjAj50A'
      2 headers = {'Authorization', 'Bearer TOKEN-HERE'}
----> 3 r = requests.get(url, headers=headers)

c:\python27\lib\site-packages\requests\api.pyc in get(url, params, **kwargs)
     68 
     69     kwargs.setdefault('allow_redirects', True)
---> 70     return request('get', url, params=params, **kwargs)
     71 
     72 

c:\python27\lib\site-packages\requests\api.pyc in request(method, url, **kwargs)
     54     # cases, and look like a memory leak in others.
     55     with sessions.Session() as session:
---> 56         return session.request(method=method, url=url, **kwargs)
     57 
     58 

c:\python27\lib\site-packages\requests\sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    472             hooks = hooks,
    473         )
--> 474         prep = self.prepare_request(req)
    475 
    476         proxies = proxies or {}

c:\python27\lib\site-packages\requests\sessions.pyc in prepare_request(self, request)
    405             auth=merge_setting(auth, self.auth),
    406             cookies=merged_cookies,
--> 407             hooks=merge_hooks(request.hooks, self.hooks),
    408         )
    409         return p

c:\python27\lib\site-packages\requests\models.pyc in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json)
    301         self.prepare_method(method)
    302         self.prepare_url(url, params)
--> 303         self.prepare_headers(headers)
    304         self.prepare_cookies(cookies)
    305         self.prepare_body(data, files, json)

c:\python27\lib\site-packages\requests\models.pyc in prepare_headers(self, headers)
    423         self.headers = CaseInsensitiveDict()
    424         if headers:
--> 425             for header in headers.items():
    426                 # Raise exception on invalid header value.
    427                 check_header_validity(header)

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

Most helpful comment

Hey @codespaced, thanks for opening this issue. It looks like you used a comma instead of a colon to separate the key and value in your header. This is creating a set object instead of the dictionary we're expecting. Replacing that should solve your issue.

All 5 comments

Hey @codespaced, thanks for opening this issue. It looks like you used a comma instead of a colon to separate the key and value in your header. This is creating a set object instead of the dictionary we're expecting. Replacing that should solve your issue.

Yup, this is strictly a Python syntax problem and not a Requests issue. Thanks @nateprewitt. :D

hey i am facing the same isuues but not able to get where i am facing attribute errors:

self.token= 'ywlGaT0X44WGEps98WDN8xyFMmJgfGTehKxT5heiq_A7yqa_xwM9z6s2ouB1R7LprEYaTtXoILzx713kYhvoOxQrPLsRuvEAhnHfpc0sRonNXRYJgD91P8c2_mJJJ58MYs2TiMCEXlByG2ODRrb9e5OA35l74HXAegMabFJE7GnLX_4ZCxD2EygFP8LKqamdfao66QXAmXW60nwWYOF7g3bPdQNH3eK2siY9yODd_pFJ2IpLxBiadEFvAWZj5Y'

self.headers = {'content-type': 'application/json',
'Authorization': 'token {}'.format(self.token)}

getting an error like :+1:
def test_api_get(self):
# A get request (json example):

  response = requests.get(self.myurl, headers=self.header)

E AttributeError: 'CL_API' object has no attribute 'header'

Please give me some inputs on it

I also have a similar error and I don't know why.

from bs4 import BeautifulSoup
import requests

URL = "https://www.amazon.com/Canon-EOS-1D-Digital-Discontinued-Manufacturer/dp/B005Y3T1AI?pf_rd_r=VM4AVQ8V3J10M7R9S7P7&pf_rd_p=be25f964-4afb-442f-819e-9e628b270a7c&pd_rd_r=9182559a-b6e0-49d1-bd12-5b729de2c9b5&pd_rd_w=AgAM5&pd_rd_wg=wTOnV&ref_=pd_gw_ci_mcx_mr_hp_d"

headers = {"User Agent:" 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}

def price_check():
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')

title = soup.find(id="productTitle").get_text()
print(title.strip())

price_check()

You have a typo in how you're writing your headers value.

headers = {"User Agent:" 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}

Should be

headers = {"User Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}

Look very closely

Was this page helpful?
0 / 5 - 0 ratings

Related issues

remram44 picture remram44  路  4Comments

JimHokanson picture JimHokanson  路  3Comments

cnicodeme picture cnicodeme  路  3Comments

NoahCardoza picture NoahCardoza  路  4Comments

justlurking picture justlurking  路  3Comments