AttributeError:"str"对象没有属性"items" [英] AttributeError: 'str' object has no attribute 'items'

查看:305
本文介绍了AttributeError:"str"对象没有属性"items"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中:

#!/usr/local/bin/python
import json

APPLICATION_NAME = 'cc9226315643df89-36bf02429075329d0ba36748360d050c'

HEADERS1 = json.dumps(dict(Destination = u"/api/af/latest/applications/%s/rulesets" % (APPLICATION_NAME)))
print "Headers1 is %s" % (HEADERS1)
HEADERS2 = {'Destination': '/api/af/latest/applications/%s/rulesets' % (APPLICATION_NAME)}
print "Headers2 is %s" % (HEADERS2)

我得到以下输出:

Headers1 is {"Destination": "/api/af/latest/applications/cc9226315643df89-36bf02429075329d0ba36748360d050c/rulesets"}
Headers2 is {'Destination': '/api/af/latest/applications/cc9226315643df89-36bf02429075329d0ba36748360d050c/rulesets'}

但是当我尝试在使用request()的REST调用中使用HEADER1或HEADER2时,得到的结果却截然不同:

but when I try to use either HEADER1 or HEADER2 in a REST call using requests(), I get very different results:

SERVER_URL = 'http://1.1.33.109:8087%s' % (APP_PATH)
REQ_DATA = None
print "Headers are: ", HEADERS
print "SERVER_URL is: ", SERVER_URL
print "Request Data is:", REQ_DATA
print ""

RESPONSE = requests.request(
    'MOVE', 
    SERVER_URL, 
    auth = ('admin', 'admin'), 
    verify = False, 
    data = REQ_DATA,
    headers = HEADERS1 )     #<-- If I use HEADER1 it breaks, if I use HEADER2 it works
print "Move Ruleset back to the Application RESULT: %s\n" % (RESPONSE)

我在HEADER1上得到了以下内容:

I get the following with HEADER1:

Traceback (most recent call last):
   File "./myrest.py", line 234, in <module>
     headers = HEADERS1 )
   File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 44, in request
     return session.request(method=method, url=url, **kwargs)
   File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 324, in request
     prep = req.prepare()
   File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 223, in prepare
     p.prepare_headers(self.headers)
   File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 340, in prepare_headers
     headers = dict((name.encode('ascii'), value) for name, value in headers.items())
AttributeError: 'str' object has no attribute 'items'

如果我使用HEADER2,它将执行干净:

If I use HEADER2 it executes cleanly:

将规则集移回应用程序结果:响应[200]

Move Ruleset back to the Application RESULT: Response [200]

任何人都可以解释这些区别是什么吗?

Can anyone explain what the differences are?

推荐答案

您正在传递字符串headers不能永远是JSON编码的字符串,它始终是Python字典.

You are passing in a string; headers can't ever be a JSON encoded string, it is always a Python dictionary.

print结果具有欺骗性; JSON编码的对象看起来很像Python字典表示形式,但是它们距离同一事物.

The print results are deceptive; JSON encoded objects look a lot like Python dictionary representations but they are far from the same thing.

requests API 明确指出必须是字典:

The requests API clearly states that headers must be a dictionary:

  • headers –(可选)与Request一起发送的HTTP标头字典.
  • headers – (optional) Dictionary of HTTP Headers to send with the Request.

JSON数据是您要作为内容发送到另一台服务器的内容,而不是用于与Python API通信的内容.

JSON data is something you'd send as content to another server, not something you'd use to communicate with a Python API.

这篇关于AttributeError:"str"对象没有属性"items"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆