Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
Found this content helpful? Log in or sign up to leave a like!
Calling url:PUT|/api/v1/courses/:course_id/quizzes/:quiz_id/questions/:id to update a quiz question answer.
When updating an existing quiz question answer using Python request library, the following error results
<Response [500]> b'{"errors":[{"message":"An error occurred.","error_code":"internal_server_error"}],"error_report_id":4595}'
When the answer portion is removed, the request works.
In the documentation, there is no layout available for the answer list. https://canvas.instructure.com/doc/api/quiz_questions.htm
Could someone provide a working example to update and existing quiz question with the answers included?
Example of API payload that produce the error:
question[position]=2&question[points_possible]=1.0&question[question_type]=multiple_choice_question&... referring to transactions and atomicity, consistency, isolation, and durability (ACID) compliance in relational databases, which statement describes transaction isolation?&question[answers]=[{'weight': 0.0, 'id': 58609.0, 'text': 'All data changes that are made to the database must be permanent as soon as a transaction is successfully completed.'}, {'weight': 0.0, 'id': 40605.0, 'text': 'All data that is written to the database as part of the transaction must adhere to all defined rules.'}, {'weight': 0.0, 'id': 9522.0, 'text': 'Each transaction either succeeds as a whole or is invalidated if any part of the transaction fails.'}, {'weight': 100.0, 'id': 25381.0, 'text': 'Each transaction is independent unto itself to ensure data concurrency and control.'}]
Example of API payload that is successful:
question[position]=2&question[points_possible]=1.0&question[question_type]=multiple_choice_question&question[question_text]=When referring to transactions and atomicity, consistency, isolation, and durability (ACID) compliance in relational databases, which statement describes transaction isolation?
It looks like the answer object has an issue: the name of the answer is "answer_text" not "text".
Unfortunately that did not do the trick. It seems there are different labels in the documentation and getting the quiz question with answer object.
In the Canvas API documentation the answer object elements are described like this:
"id": 6656, "answer_text": "Constantinople", "answer_weight": 100,
However, when I get the answers from existing quiz question in Canvas it looks like this:
'answers': [{'weight': 100, 'migration_id': 'RESPONSE_67159', 'id': 67159, 'text': 'A computer network is a collection of computing devices that are logically connected to communicate and share resources.'}
Update output
question[answers]=[{'id': 58609, 'answer_text': 'All data changes that are made to the database must be permanent as soon as a transaction is successfully completed.', 'answer_weight': 0}, {'id': 40605, 'answer_text': 'All data that is written to the database as part of the transaction must adhere to all defined rules.', 'answer_weight': 0}, {'id': 9522, 'answer_text': 'Each transaction either succeeds as a whole or is invalidated if any part of the transaction fails.', 'answer_weight': 0}, {'id': 25381, 'answer_text': 'Each transaction is independent unto itself to ensure data concurrency and control.', 'answer_weight': 100}] <Response [500]> b'{"errors":[{"message":"An error occurred.","error_code":"internal_server_error"}],"error_report_id":5052}'
How can I raise this question with Canvas?
Unfortunately not - the payload producing the error id is set as an integer:
&question[answers]=[{'id': 8541, 'answer_text': 'It ensures that resources are accessed by only authorized users access.', 'answer_weight': 100}, {'id': 56090, 'answer_text': 'It ensures that resources do not contain private information.', 'answer_weight': 0}, {'id': 1361, 'answer_text': 'It ensures that resources are accessible when they are needed.', 'answer_weight': 0}] b'{"errors":[{"message":"An error occurred.","error_code":"internal_server_error"}],"error_report_id":3858}
Is it possible for someone to put up an example for updating the answer? I have the same 500 error and it doesn't seem to be able to update the answer no matter what combinations I tried. Actually, I just want to restrict the answer range from 1-5 automatically. If I can do it while adding the question, it would be fine for me as well. But I have even less luck adding questions with the instructions on the official Canvas API page.
# Set up headers for the request, including the API key for authentication
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# Define the data for the comment
data = {
# 'comment[text_comment]': COMMENT
'question[question_name]': 'an IMPORTANT question',
'question[question_text]': 'Will Canvas eventually fix this BUG?',
'question[question_type]':'numerical_question',
'question[points_possible]':'2',
'question[answers]': [ { 'numerical_answer_type':'exact_answer', 'exact':42}]
# 'precision':1,
# 'start':1,
# 'end':5}]
}
# Make the POST request to the Canvas API to create the submission comment
response = requests.post(url, headers=headers, json=data)
It didn't return an error. But it doesn't even update the question title and text as desired. Basically, it just created a template question. I used canvasapi and it managed to create a question with the correct title and content but I can't restrict the answer range with that. Any help is greatly appreciated!
I came across your post because I encountered the same issue, and the errors and omissions in the Canvas documentation certainly didn't help. I was somewhat relieved that the post was relatively recent so it might be useful.
While searching on the Canvas GitHub, I discovered a semi-related issue here: https://github.com/instructure/canvas-lms/issues/2045. The person mentions that the call to update works for them, except for the 'comments' field (it worked when they omitted this field). I checked their payload and it contained the answers (ha!):
The correct format for the 'answers' in the payload is of the form: 'question[answers][#][property]', so in the example you post, it should be something like this:
question[position]=2&
question[points_possible]=1.0&
question[question_type]=multiple_choice_question&
question[question_text]=When referring to transactions and atomicity, consistency, isolation, and durability (ACID) compliance in relational databases, which statement describes transaction isolation?&
question[answers][0][weight]=0.0&
question[answers][0][id]=58609&
question[answers][0][text]=All data changes that are made to the database must be permanent as soon as a transaction is successfully completed&
question[answers][1][weight]=0.0&
question[answers][1][id]=40605&
question[answers][1][text]=All data that is written to the database as part of the transaction must adhere to all defined rules&
question[answers][2][weight]=0.0&
question[answers][2][id]=9522&
question[answers][2][text]=Each transaction either succeeds as a whole or is invalidated if any part of the transaction fails&
question[answers][3][weight]=100.0&
question[answers][3][id]=25381&
question[answers][3][text]=Each transaction is independent unto itself to ensure data concurrency and control
In my case, I was using python so I converted it to a dict like @samuel_cheng1, but with the field format for answers that I found:
data = {...,
'question[answers][0][numerical_answer_type]':'range_answer',
'question[answers][0][answer_range_start]':1,
'question[answers][0][answer_range_end]':5
}
and I was able to update the answers of a numerical range quiz without problems.
If you don't find the correct property name in the docs, check the classes used here: https://github.com/icelab/canvas/blob/master/app/views/quizzes/_display_answer.html.erb (I found there that the name of the field 'start' is actually 'answer_range_start', for example)
Hope it helps!
To participate in the Instructure Community, you need to sign up or log in:
Sign In