2016-02-13 55 views
0

我工作的一个项目,包括Verizon的Thingspace的REST API到Python程序。使用JSON数据问题在Python

一位同事在卷曲的作品为例(我不熟悉的卷曲所以我试图转换成Python)为我提供。

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "VZ-M2M-Token: 621d9779-f8bc-4fe9-91dd-b726c52e7117" --header "Authorization: Bearer 89ba225e1438e95bd05c3cc288d3591" -d "{\"accountName\": \"TestAccount-1\"}" https://thingspace.verizon.com/api/m2m/v1/devices/actions/list 

我想把这个完全相同的请求转换成Python函数。下面是我有:

import requests 

def getList(token): 
    url = "https://thingspace.verizon.com/api/m2m/v1/devices/actions/list" 
    headers = { 
       "Content-Type": "application/json", 
       "Accept": "application/json", 
       "Authorization": "Bearer 89ba225e1438e95bd05c3cc288d3591", 
       "VZ-M2M-Token": "f7ef3a35-abb6-418b-92d4-7cdac8b06c5f", 
      } 
    data = {"accountName": "TestAccount-1"} 

    print data 

    deviceList = requests.post(url, data=data, headers=headers) 

    print headers 
    print (deviceList.status_code, deviceList.reason, deviceList.text) 
    return deviceList 

当我运行它,我得到的JSON以下错误消息回:

(400, '错误的请求', U'{ “错误码”:” REQUEST_FAILED.UnexpectedError “ ”的errorMessage“:” 难道 无法读取文件:无法识别的记号\ '帐户名\':期待 (\ '真正的\',\ '假\' 或\ '空\')\ n在[来源: [email protected];行:1,柱:13];嵌套 例外是com.fasterxml.jackson.core.JsonParseException: 无法识别的令牌\ '帐户名\':期待(\ '真\' \ '假\' 或\ '空\')\ n在[来源:[email protected]; 线:1,列:13]“}”)

您可以参考这里的API的信息:https://thingspace.verizon.com/developer/apis#/Connectivity%20Management/API%20Reference/Retrieve%20Device%20Information.html

我相信有可能是坏了我的JSON字符串,但我需要另一套的眼睛。

回答

0

data不会自动转换为JSON,你必须明确地做到这一点:

deviceList = requests.post(url, data=json.dumps(data), headers=headers) 
+0

嗯,这是快速,正确率100%太谢谢你了! –