Getting the list of modules of a course via a command
In "CANBASH: Canvas & BASH Scripting", a Blog Post created by kent Links to an external site. on Jul 2, 2015 https://community.canvaslms.com/groups/canvas-developers/blog/2015/07/02/canbash-canvas-bash-scripting Links to an external site.
he shows an example of requesting a listing of all the modules in a course as:
curl -H 'Authorization: Bearer <token>' \
https://<canvas>/api/v1/courses/1234/modules
I tried on a set of modules:
I could access them with this command:
curl -H 'Authorization: Bearer 8..K' https://kth.instructure.com/api/v1/courses/11/modules
and got the JSON reply:
[{"id":13,"name":"Old course material","position":1,"unlock_at":null,"require_sequential_progress":false,"publish_final_grade":false,"prerequisite_module_ids":[],"published":false,"items_count":0,"items_url":"https://kth.instructure.com/api/v1/courses/11/modules/13/items"},{"id":12,"name":"Module 1","position":2,"unlock_at":null,"require_sequential_progress":false,"publish_final_grade":false,"prerequisite_module_ids":[],"published":false,"items_count":3,"items_url":"https://kth.instructure.com/api/v1/courses/11/modules/12/items"}]
This reply can be pretty-printed by feeding it to:
python -m json.tool
thus producing:
[
{
"id": 13,
"items_count": 0,
"items_url": "https://kth.instructure.com/api/v1/courses/11/modules/13/items",
"name": "Old course material",
"position": 1,
"prerequisite_module_ids": [],
"publish_final_grade": false,
"published": false,
"require_sequential_progress": false,
"unlock_at": null
},
{
"id": 12,
"items_count": 3,
"items_url": "https://kth.instructure.com/api/v1/courses/11/modules/12/items",
"name": "Module 1",
"position": 2,
"prerequisite_module_ids": [],
"publish_final_grade": false,
"published": false,
"require_sequential_progress": false,
"unlock_at": null
}
]
Note: The CANBASH blog posting Links to an external site. shows how to make a bash shell script to automate the above.
If you ask for a non-existent course id you will get an error message of the form:
{"errors":[{"message":"The specified resource does not exist."}],"error_report_id":135}
In the JSON reply for a valid course ID we we can see the list consists of two items. The first is an item with no items within it, but the second item (
https://kth.instructure.com/api/v1/courses/11/modules/12/items)
has 3 subitems. We can get these and pretty-print them with curl getting:
[
{
"html_url": "https://kth.instructure.com/courses/11/modules/items/20",
"id": 20,
"indent": 0,
"module_id": 12,
"page_url": "test-1",
"position": 0,
"published": false,
"title": "Test 1 ",
"type": "Page",
"url": "https://kth.instructure.com/api/v1/courses/11/pages/test-1"
},
{
"html_url": "https://kth.instructure.com/courses/11/modules/items/21",
"id": 21,
"indent": 1,
"module_id": 12,
"page_url": "test-2",
"position": 1,
"published": false,
"title": "Test 2 ",
"type": "Page",
"url": "https://kth.instructure.com/api/v1/courses/11/pages/test-2"
},
{
"id": 22,
"indent": 1,
"module_id": 12,
"position": 2,
"published": false,
"title": "Header 1 ",
"type": "SubHeader"
}
]
In the above, we can see that the first subitem ("test-1") was not indented (i.e., an indent of zero), while the other two items ("test-2" and "Header 1 ") we indented by 1. Note also that the second of these has a trailing space in the title!
In order to understand the details of a module item, see https://canvas.instructure.com/doc/api/modules.html Links to an external site.