Reputation: 230
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)
Upvotes: 1
Views: 1480
Reputation: 2821
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)
Upvotes: 2