I tried the example both from https://api.qualtrics.com/guides/reference/surveyDefinitions.json/paths/~1survey-definitions~1%7BsurveyId%7D~1questions/post and https://api.qualtrics.com/guides/docs/Guides/Common%20Tasks/building-surveys.md , but none of them works.
How do I create a question using python code? Thanks!
Hey xxbidiao,
I use this API call to create questions in Python, and it works fine for me.
One thing I found helpful was to use GetQuestion on an existing question in the survey to pull the question definition, in order to understand how CreateQuestion expects the JSON body to look.
This is what my python function looks like:
import requests
def createQuestion(surveyId,blockId,payload):
url = "{0}survey-definitions/{1}/questions?blockId={2}".format(Q_baseUrl,surveyId,blockId)
response = requests.post(url, headers=Q_headers, data=json.dumps(payload))
if response.json()['meta']['httpStatus'] != "200 - OK":
print("API Error - See response:",response.json())
print("API Error - See url:",url)
print("API Error - See payload:",json.dumps(payload))
raise Exception("API Error")
return response
Something that I found was important was to remember to convert the payload to JSON. In line 3 you can see I use
json.dumps(payload)
Sadly, I tried copying the exact structure I got from GET survey-definitions/{surveyID}/questions/ and made sure that the "payload" (was a dict) is
json.dumps(payload)'ed but with no luck.
I still get `'httpStatus': '400 - Bad Request', 'error': {'errorCode': 'QMST_1', 'errorMessage': 'The request was invalid.', 'code': 'ESRV115', 'message': 'The provided JSON did not match the required schema.'}}}`
This is the structure I was posting to the server (a minor change from the first default question when creating survey on the website:
data = dict(QuestionText=qtext, DataExportTag='Q1', QuestionType='MC', Selector='SAVR', SubSelector='TX',
Configuration={'QuestionDescriptionOption': 'UseText'},
QuestionDescription='Click to write the question text',
Choices={'1': {'Display': 'Click to write Choice 1'}, '2': {'Display': 'Click to write Choice 2'},
'3': {'Display': 'Click to write Choice 3'}}, ChoiceOrder=['1', '2', '3'],
Validation={'Settings': {'ForceResponse': 'OFF', 'Type': 'None'}}, Language=[], NextChoiceId=4,
NextAnswerId=1, QuestionID='QID1', QuestionText_Unsafe='Click to write the question text')
return data
Any suggestions?
I don't know what else to tell you then.... are your headers correct? Are you including
"Content-Type": "application/json"in your request header?
Personally, I construct my data like this and it's working for me.
data = {
"QuestionText": qtext,
"DataExportTag": "Q1",
"QuestionType": "MC",
"Selector": "SAVR",
"SubSelector": "TX",
"Configuration": {
"QuestionDescriptionOption": "UseText"
},
"QuestionDescription": "Click to write the question text",
"Choices": {
"1": {
"Display": "Click to write Choice 1"
},
"2": {
"Display": "Click to write Choice 2"
},
"3": {
"Display": "Click to write Choice 3"
}
},
"ChoiceOrder":
"1",
"2",
"3"
],
"Validation": {
"Settings": {
"ForceResponse": "OFF",
"Type": "None"
}
},
"Language":],
"NextChoiceId":4,
"NextAnswerId":1
Also, some notes:
- When creating a new question, you should not include 'QuestionID' as Qualtrics will generate a new one regardless of what you provide.
- When creating a new question, you should not include 'QuestionText_Unsafe', (this is Qualtrics generated on getQuestion I believe).
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.