cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
b_kheyfets
New Member

How do I create quiz question via API?

Jump to solution

Hello,

I'm trying to create a question to an existing quiz via API.

The doc is here: Quiz Questions - Canvas LMS REST API Documentation 

So I made a simple data.json:

{
  "question": {
    "question_name": "Question",
    "question_text": "<p>test</p>",
    "question_type": "essay_question",
    "points_possible": 1
  }
}

and send it to the canvas:

curl -s 'https://canvas.url/api/v1/courses/1571/quizzes/517/questions' -X POST -d @data.json   -H "Authorization: Bearer $CANVAS_TOKEN" | jq -r '.'

But canvas ignored all my fields, and created a dummy question instead:

{
  "id": 1446,
  "quiz_id": 517,
  "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
}

Could it be the case of question API does not work?

Tags (1)
1 Solution
James
Community Champion

 @b_kheyfets , 

Your solution does not match my experience with Canvas, so I tested your original JSON on my sandbox. I used the Advanced Rest Client rather than curl and it worked as expected.

I think what you're missing is that when you send JSON content, you need to add the content-type: application/json header.

To verify this, I did some testing.

In my original request that worked, I had the Authorization header, but I also had

accept: application/json
content-type: application/json

If I take out the content-type header, then I get the same problem that you had where it ignores all of my information.

The accept header isn't necessary, but it doesn't hurt. I took it out and as long as the content-type was there, then the request worked properly.

View solution in original post

3 Replies
b_kheyfets
New Member

Solved it:

 curl -s 'https://canvas.url/api/v1/courses/1571/quizzes/517/questions' \
 -X POST \
 -F 'question[question_name]=Question' \
 -F 'question[question_text]= <p>test</p>' \
 -F 'question[question_type]=essay_question' \
 -F 'question[points_possible]=1' \
 -H "Authorization: Bearer $CANVAS_TOKEN" | jq -r '.'

works. Note the space before <p> -- it won't work without it. I tried to add it to the data.json file I send to the server:

 {
  "question": {
  "question_name": "Question",
  "question_text": " <p>test</p>",
  "question_type": "essay_question",
  "points_possible": 1
 }
}

-- this does not work.

James
Community Champion

 @b_kheyfets , 

Your solution does not match my experience with Canvas, so I tested your original JSON on my sandbox. I used the Advanced Rest Client rather than curl and it worked as expected.

I think what you're missing is that when you send JSON content, you need to add the content-type: application/json header.

To verify this, I did some testing.

In my original request that worked, I had the Authorization header, but I also had

accept: application/json
content-type: application/json

If I take out the content-type header, then I get the same problem that you had where it ignores all of my information.

The accept header isn't necessary, but it doesn't hurt. I took it out and as long as the content-type was there, then the request worked properly.

Indeed it works:

curl -s 'https://canvas.url/api/v1/courses/1571/quizzes/519/questions' \
-X POST \
-d @data.json \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $CANVAS_TOKEN" | jq -r '.'

Thanks!