API 通话中没有输入

Nonetype on API call

使用 http.client 从 Qualtrics 导出调查回复。为了获得我的调查回复,我需要进度 ID...当我创建数据导出时,我能够看到它的结果,但在尝试获取我需要的值时出现 NoneType 错误:

import http.client

baseUrl = "https://{0}.qualtrics.com/API/v3/surveys/{1}/export-responses/".format(dataCenter, surveyId)
headers = {
    "content-type": "application/json",
    "x-api-token": apiToken
}
downloadRequestPayload = '{"format":"' + fileFormat + '","useLabels":true}'

downloadRequestResponse = conn.request("POST", baseUrl, downloadRequestPayload, headers)
downloadRequestResponse

{"result":{"progressId":"ES_XXXXXzFLEPYYYYY","percentComplete":0.0,"status":"inProgress"},"meta":{"requestId":"13958595-XXXX-YYYY-ZZZZ-407d23462XXX","httpStatus":"200 - OK"}}

所以我清楚地看到了我需要的 progressId 值但是当我试图抓住它时...

progressId = downloadRequestResponse.json()["result"]["progressId"]
AttributeError: 'NoneType' object has no attribute 'json'

(我知道我可以使用 Qualtrics 建议的请求库,但出于我的目的,我需要使用 http.client 或 urllib)

请看https://docs.python.org/3/library/http.client.html#http.client.HTTPConnection

conn.request 没有 return 任何东西(在 Python 中意味着它 returns None,这就是你的错误发生的原因).

要获得响应,请使用 getresponse,在发送请求后调用它时,return 是一个 HTTPResponse 实例。

另外,请注意 HTTPResponse 对象中没有 json 方法。不过,有一个 read 方法。您可能需要使用 json 模块来解析内容。

...
import json
conn.request("POST", baseUrl, downloadRequestPayload, headers)
downloadRequestResponse = conn.getresponse()
content = downloadRequestResponse.read()
result = json.loads(content)