AttributeError:'str'对象没有属性'items'

时间:2013-09-18 08:59:27

标签: python string rest dictionary http-request

在以下代码中:

#!/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'}

但是当我尝试使用requests()在REST调用中使用HEADER1或HEADER2时,我会得到非常不同的结果:

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获得以下内容:

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,它会干净地执行:

  

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

有谁可以解释这些差异是什么?

3 个答案:

答案 0 :(得分:13)

您正在传递字符串; headers 永远不会成为JSON编码的字符串,它始终是Python字典。

print结果具有欺骗性; JSON编码对象看起来很像Python字典表示,但它们来自同一件事。

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

  
      
  • headers - (可选)使用Request发送的HTTP标头字典。
  •   

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

答案 1 :(得分:1)

我遇到了这个问题,我需要使用内容类型创建标头,并将数据元素作为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

答案 2 :(得分:0)

您可以通过{

  'Content-Type': 'application/json;charset=UTF-8'
}

有效