繁体   English   中英

我收到 python 错误 - AttributeError: 'str' object has no attribute 'items'

[英]i am getting python error - AttributeError: 'str' object has no attribute 'items'

在这里需要一些帮助。 我正在尝试在我的请求中发送带有 header 的 JWT 令牌。 当我直接将令牌复制粘贴到请求中时(第 6 行),它工作正常。 当我连接为字符串并发送(第 3 行和第 5 行)时,它会抛出错误。 但是当打印令牌时它具有正确的令牌值。我在代码之后粘贴的错误

response = requests.post("some URL")
token = response.text
header_content = "{'Authorization': 'Bearer "+token+"'}"
print(header_content)
response = requests.get(url, headers=header_content)
#response = requests.get(url, headers = {'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlxdWVfbmFtZSI6ImhjbF9yZXBvcnRpbmcuc2VydmljZWFjY291bnRAc2x1LnRlYW1keW5hbWl4LmNvbSIsInRkeF9lbnRpdHkiOiIyIiwidGR4X3BhcnRpdGlvbiI6IjcwIiwiaXNzIjoiVEQiLCJhdWQiOiJodHRwczovL3d3dy50ZWFtZHluYW1peC5jb20vIiwiZXhwIjoxNjIyOTgyNzg5LCJuYmYiOjE2MjI4OTYzODl9.82FuBtybRBk3Ot-whKYEXw2yFeNXBp566MubEA9G-BE'})

the error i am seeing is

Traceback (most recent call last):
File "C:\Users\prasannakumaravel\PycharmProjects\SLU_DailyReport_Automation\Rest_Testing.py", line 24, in <module>
response = requests.get(url, headers=header_content)
File "C:\Users\prasannakumaravel\PycharmProjects\SLU_DailyReport_Automation\venv\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\prasannakumaravel\PycharmProjects\SLU_DailyReport_Automation\venv\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\prasannakumaravel\PycharmProjects\SLU_DailyReport_Automation\venv\lib\site-packages\requests\sessions.py", line 528, in request
prep = self.prepare_request(req)
File "C:\Users\prasannakumaravel\PycharmProjects\SLU_DailyReport_Automation\venv\lib\site-packages\requests\sessions.py", line 456, in prepare_request
p.prepare(
File "C:\Users\prasannakumaravel\PycharmProjects\SLU_DailyReport_Automation\venv\lib\site-packages\requests\models.py", line 317, in prepare
self.prepare_headers(headers)
File "C:\Users\prasannakumaravel\PycharmProjects\SLU_DailyReport_Automation\venv\lib\site-packages\requests\models.py", line 449, in prepare_headers
for header in headers.items():
AttributeError: 'str' object has no attribute 'items'

header_content应该是字典而不是字符串

改变这个

header_content = "{'Authorization': 'Bearer "+token+"'}"

header_content = {'Authorization': "Bearer "+token}

您可以从错误 Traceback 中跟踪它:

for header in headers.items():
AttributeError: 'str' object has no attribute 'items'

.items()是字典的属性, not string

在你的情况下header.items()

[('Authorization', 'Bearer your_token')]

这里的问题是 requests 需要一个字典,而不是类似于 python 字典的字符串。 在错误消息的最后第二行中,它for header in headers.items():这是一个 python 字典方法。 要解决这个问题,您可以使用 python 中的 json 模块。

import json
header_content = '{"Authorization": "Bearer " + "some_token"}'
header_content = dict(json.loads(header_content)
response = requests.get(url, headers = header_content)

注意:由于某种原因 json 需要用"而不是'括起来的内容。

我不太确定你使用什么框架,但通常有一个单独的 function 来添加标题,而不是仅仅将它们作为字符串添加。

根据这个答案,在带有 urllib2 的 Python 中,它将是:

import urllib2
req = urllib2.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
resp = urllib2.urlopen(req)
content = resp.read()

对于带有快递的 node.js,在此答案中进行了解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM