从httplib.HTTPSConnection请求返回的数据格式无效

时间:2014-08-15 00:24:43

标签: python json api response httplib

#!/usr/local/bin/python2.7

import httplib, urllib

params = urllib.urlencode({"username":"api", "password":"cisco123", "data":{"token":""}})
headers = {"Content-Type":"application/json", "Accept":"application/json"}

conn = httplib.HTTPSConnection("10.86.79.93", 443)
conn.request("GET", "/restapi/16/authenticate", params, headers)

response = conn.getresponse()

print "\n"

print response.status, response.reason

data = response.read()

print "\n"

print data

print "\n"

print response.msg

print "\n”

Output:

The response data is looking for JSON and here is the example they give:


“data”:{“token”:”1234567890xxxxxxxxxxxxxxxxxxxxx”}

200 OK


{"status":100,"message":"Invalid data format.","data":null,"envelope_version":"16"}
  • 日期:2014年8月15日(星期五)00:10:49 GMT

  • 服务器:Apache / 2.2.15(CentOS)

  • X-Powered-By:PHP / 5.4.14

  • 内容长度:83

  • 连接:关闭

  • Content-Type:text / html;字符集= UTF-8

知道为什么我会在数据响应中收到“无效数据格式”消息并且为空?

1 个答案:

答案 0 :(得分:0)

我相信urllib.urlencode不能很好地处理嵌套的json。您在“data”中有一个嵌套对象:{“token”:“”}。看看urlencode a multidimensional dictionary in python 寻求解决方案。