Register for InstructureCon25 • Passes include access to all sessions, the expo hall, entertainment and networking events, meals, and extraterrestrial encounters.
I've been attempting to create quizzes via the canvas api, but am running into issue (exacerbated by the less-than-ideal error handling by the canvas api).
I'm able to create a quiz, but when trying to create a quiz question, I've run into the following two errors:
- A 500 error with no detail on the payload issue
- A 200, but none of the parameters I'm passing are used and I'm left with a blank quiz question
Here is a sample request:
r = requests.post(
url + '/v1/courses/' + str(course_id) + '/quizzes/' + str(quiz_id) + '/questions',
headers = auth,
json = question
)
where...
question = {
"question[question_name]" : "Question 1",
"question[question_text]" : "This is a question, pick an answer",
"question[question_type]" : "multiple_choice_question",
"question[points_possible]" : 1,
"question[answers]" : [
{
"answer_text" : "It worked!",
"answer_weight" : 100
},
{
"answer_text" : "This did not work",
"answer_weight" : 0
}
]
}
Here is the response I get:
200 { "id": 160769, "quiz_id": 48977, "quiz_group_id": null, "assessment_question_id": null, "position": null, "question_name": "Question", "question_type": "text_only_question", "question_text": "Question text", "points_possible": 0, "correct_comments": "", "incorrect_comments": "", "neutral_comments": "", "correct_comments_html": "", "incorrect_comments_html": "", "neutral_comments_html": "", "answers": [], "variables": null, "formulas": null, "answer_tolerance": null, "formula_decimal_places": null, "matches": null, "matching_answer_incorrect_matches": null, "assessment_question": null }
Any help here would be appreciated.
Hello @ts1994 ,
Thank you for contacting us. I looked over this and it would be est to look over the create quiz API here to make sure you have the correct lines included https://canvas.instructure.com/doc/api/quizzes.html#method.quizzes/quizzes_api.create
I have the same issue. Passing data in JSON format does not produce an error, but it creates a generic quiz, as you added in your post.
I have a partial answer with urlencode. The tricky part is passing [answers], which is a list of dictionaries. On https://canvas.instructure.com/doc/api/, they describe how to pass a list, but there is no discussion when the entries of a list are dictionaries.
Would you try the following?
question = "question[question_name]=Question 1&question[question_text]=This is a question, pick an answer&question[question_type]=multiple_choice_question&question[points_possible]=1&question[answers][][answer_text]=It worked!&question[answers][][answer_weight]=100&question[answers][][answer_text]=This did not work"
r = requests.post(
url + '/v1/courses/' + str(course_id) + '/quizzes/' + str(quiz_id) + '/questions',
headers = auth,
data = question
)
json is changed to data. Your headers should not have content-type json. Here is the response I received.
[{'id': 1925282,
'quiz_id': 108665,
'quiz_group_id': None,
'assessment_question_id': 2662408,
'position': None,
'question_name': 'Question 1',
'question_type': 'multiple_choice_question',
'question_text': 'This is a question, pick an answer',
'points_possible': 1.0,
'correct_comments': '',
'incorrect_comments': '',
'neutral_comments': '',
'correct_comments_html': '',
'incorrect_comments_html': '',
'neutral_comments_html': '',
'answers': [{'id': 6961,
'text': 'It worked!',
'html': '',
'comments': '',
'comments_html': '',
'weight': 100.0},
{'id': 6607,
'text': 'This did not work',
'html': '',
'comments': '',
'comments_html': '',
'weight': 0.0}],
'variables': None,
'formulas': None,
'answer_tolerance': None,
'formula_decimal_places': None,
'matches': None,
'matching_answer_incorrect_matches': None}]
To interact with Panda Bot in the Instructure Community, you need to sign up or log in:
Sign In
This discussion post is outdated and has been archived. Please use the Community question forums and official documentation for the most current and accurate information.