cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
a_mottura
Community Participant

Adding answers to multiple choice quiz questions via Canvas API

Jump to solution

Hi,

I am trying to use the Canvas API to create quizzes on Canvas. I have managed to create quizzes in a course, and I have managed to add questions to the quizzes I created - no problems there. I am stuck at adding answers to multiple choice questions (either at the time of creating the question, or editing the question after creation).

The documentation (Quiz Questions - Canvas LMS REST API Documentation) does not seem to say very much, other than question[answers] should be an array. I have tried different structures for the data, and got nowhere. I used the Canvas Live API facility we have, and there is nothing I can write in the text box that returns a success code (I get a 500 server error as soon as I add a non-empy question[answers] array.

As an example, here is the payload (in Python) I pass to my POST request to create a question with answers:

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]': 0,
'question[answers]': [
{
'answer_text': 'Right answer',
'answer_weight': 100
},
{
'answer_text': 'Wrong answer',
'answer_weight': 0
}
]
}

Any ideas? If I remove the question[answers] item from the dictionary, the POST request works fine. If I add it, I get an internal server error. Am I missing something obvious?

I get the same exact behaviour when I use the Canvas Live API. If I select the function to create a quiz question, the page displays the input parameters. I can use this successfully to create a quiz question, but there is nothing I managed to write in the box below that resulted in the creation of a multiple choice quiz question with answers.

316966_quizapi.jpeg

Thanks,

Alessandro

1 Solution

Accepted Solutions
James
Community Champion

What you have looks very similar to what I use. I have blank_id specified for multiple choice and multiple drop down questions, but I don't know if it's required. I think I reverse engineered it from the network call that Canvas made when it created questions.

I do use JSON when I send the information.  That means you'll need to include a Content-type: application/json header.

If you're not sending it as JSON, you might change the way your request is formatted.

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]': 0,
'question[answers][0]['answer_text']: 'Right answer',
'question[answers][0]['answer_weight']: 100,
'question[answers][1]['answer_text']: 'Wrong answer',
'question[answers][1]['answer_weight']: 0
}

View solution in original post

3 Replies
pklove
Community Champion

Have you tried sending it as pure JSON?

Sending the answers in JSON format as the value for the post parameter might not work.

James
Community Champion

What you have looks very similar to what I use. I have blank_id specified for multiple choice and multiple drop down questions, but I don't know if it's required. I think I reverse engineered it from the network call that Canvas made when it created questions.

I do use JSON when I send the information.  That means you'll need to include a Content-type: application/json header.

If you're not sending it as JSON, you might change the way your request is formatted.

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]': 0,
'question[answers][0]['answer_text']: 'Right answer',
'question[answers][0]['answer_weight']: 100,
'question[answers][1]['answer_text']: 'Wrong answer',
'question[answers][1]['answer_weight']: 0
}

View solution in original post

a_mottura
Community Participant

Thanks! This works! I was not sending it as JSON, so structuring the data as above worked!

🙂

Thanks all!