Quiz question creation via curl/JSON error

Jump to solution
MichaelFerero
Community Member

I'm using curl to send create quiz questions to an already create quiz, the JSON is as follows:

{ "question": {
"question_name": "Question 3",
"question_text": "Two of the basic instructions implemented by a programming language include:",
"question_type": "multiple_choice_question",
"points_possible": 2,
"answers": [
{
"answer_html": "input and resource allocation",
"answer_weight": 0
},
{
"answer_html": "math and string processing",
"answer_weight": 0
},
{
"answer_html": "output and monitoring program execution",
"answer_weight": 0
},
{
"answer_html": "conditional execution and repetition",
"answer_weight": 100
}
]
}
}

When I attempt to upload it using this curl command:

curl 'https://vccs.instructure.com/api/v1/courses/22246/quizzes/3106210/questions' \
--trace q.output \
-X POST \
-d @q3.json \
-H "Content-type: application/json" \
-H "Authorization: Bearer $CANVAS_TOKEN" > q1.html

I get the following error:

{"errors":[{"message":"An error occurred.","error_code":"internal_server_error"}],"error_report_id":6575670}

Now, if I remove the "answers" piece, it works . . . but I can't see anything wrong with it.  I've looked for UTF-8 characters . . . tabs  . . . nothing there

It is very possible I've stared at this for so long I can't see the issue.

 

Labels (1)
0 Likes
2 Solutions
James
Community Champion

@MichaelFerero 

The structure of the Answers object is a bit ambiguous. It is an array of answers, but it needs the index in there and just sending the JSON object doesn't do that for you.

One way to get the keys is to create an object and use the index as the property name.

{
  "question": {
    "question_name": "Question 3",
    "question_text": "Two of the basic instructions implemented by a programming language include:",
    "question_type": "multiple_choice_question",
    "points_possible": 2,
    "answers": {
      "0": {
        "answer_text": "input and resource allocation",
        "answer_weight": 0
      },
      "1": {
        "answer_text": "math and string processing",
        "answer_weight": 0
      },
      "2": {
        "answer_text": "output and monitoring program execution",
        "answer_weight": 0
      },
      "3": {
        "answer_text": "conditional execution and repetition",
        "answer_weight": 100
      }
    }
  }
}

 

I used answer_text instead of answer_html because there was no html in your text. If you're sending answer_html, you should wrap your text in HTML elements. By default, Canvas uses <p>...</p>

To figure out information like this, I use the developer tools in Chrome and look at the network traffic that is sent when I perform the action within Canvas. Then I know what the format should look like. What I saw was stuff like this:

questions[answers][0][answer_text]
questions[answers][0][answer_weight]

There was a bunch more, but that let me know what it should look like and the rest of the junk was junk -- it was for other types of questions.

View solution in original post

0 Likes
jackyjoy123
Community Member