AnsweredAssumed Answered

Canvas API/Python Help - POST Requests

Question asked by Jason Kocur on Oct 24, 2019
Latest reply on Oct 29, 2019 by Brian Bennett

Hi Everyone!

 

I hope you are all doing well! I have recently started learning python and have admin access to our universities API. However, I have been finding it challenging to do some of the basic tasks that could help create efficiencies by automating things. I am able to pull some basic reports, but what I'm really looking for is how to send POST requests to update things like Quiz Extensions, as an example. It doesn't seem like there is anything out there on how to translate the Canvas API documentation into python. As a result of my guesses, I keep ending up with a 422 error code. The example below shows a what I have gotten so far, if anyone has a suggestion, that would be amazing!

 

# Use Python to POST Quiz Extentions to CANVAS API

# Import libraries
import requests
import json

# Headers(token)

headers = {
'Authorization': 'Bearer <token>'
}

payload = {
"quiz_extensions": [{
"user_id": 72448,
"extra_attempts": 2,
"manually_unlocked": True

}]}

payload_json = json.dumps(payload)
print(payload_json)


api_url = 'https://<school>.instructure.com/api/v1/accounts/1/courses/53176/quizzes/198830/extensions'

# Requests
r = requests.post(api_url, data = payload_json, headers = headers)

print(r.status_code)

Outcomes