Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

python - Nonetype on API call

using http.client to export survey responses from Qualtrics. In order to get my survey responses I required the progress Id... when I create the data export I am able to see the result of it but get NoneType error when trying to get the value I need:

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"}}

So I clearly see the progressId value I need but when I try to grab it...

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

(I know I can use the requests library Qualtrics suggests but for my purpose I need to use http.client or urllib)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please see https://docs.python.org/3/library/http.client.html#http.client.HTTPConnection

conn.request doesn't return anything (which in Python, means it returns None, and this is why your error is happening).

To get the response, use getresponse, which when called after a request is sent, returns an HTTPResponse instance.

Also, note that there is no json method in an HTTPResponse object. There is a read method, though. You may need to use the json module to parse the contents.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...