Last active
October 27, 2020 02:31
-
-
Save ggorlen/cbb88f48a31643070762ce726407bf76 to your computer and use it in GitHub Desktop.
Add a file to a submission in the Canvas LMS API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| token = "YOUR CANVAS API TOKEN" | |
| filename = "test.txt" | |
| file_url = "https://gatech.instructure.com/api/v1/courses/137794/assignments/537670/submissions/26370" | |
| url = file_url + "/comments/files" | |
| headers = {"Authorization": f"Bearer {token}"} | |
| data = { | |
| "name": filename, | |
| "content_type": "text/plain", | |
| } | |
| res = requests.post(url, data=data, headers=headers) | |
| with open(filename, "rb") as f: | |
| res = requests.post( | |
| res.json()["upload_url"], | |
| data={"key": filename}, | |
| files={"file": f} | |
| ) | |
| data = { | |
| "key": filename, | |
| "comment[file_ids][]": res.json()["id"], | |
| } | |
| res = requests.put(file_url, data=data, headers=headers) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # https://canvas.instructure.com/doc/api/file.file_uploads.html | |
| # https://canvas.instructure.com/doc/api/submissions.html | |
| # https://canvas.instructure.com/doc/api/submission_comments.html | |
| token='YOUR CANVAS API TOKEN' | |
| file='test.txt' | |
| url='https://gatech.instructure.com/api/v1/courses/137794/assignments/537670/submissions/26370/comments/files' | |
| file_url='https://gatech.instructure.com/api/v1/courses/137794/assignments/537670/submissions/26370' | |
| res=$(curl -s -X POST $url \ | |
| -F "name=$file" \ | |
| -F 'content_type=text/plain' \ | |
| -H "Authorization: Bearer $token") | |
| upload_url=$(echo $res | jq -r .upload_url) | |
| res=$(curl -s $upload_url \ | |
| -F "key=$file" \ | |
| -F "file=@$file") | |
| file_id=$(echo $res | jq -r .id) | |
| curl -s -X PUT $file_url \ | |
| -F "comment[file_ids][]=$file_id" \ | |
| -H "Authorization: Bearer $token" > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment