List active courses in account not paginating?

Jump to solution
ek
Community Member

Accounts - Canvas LMS REST API Documentation 

I've been using the API endpoint: 

GET /api/v1/accounts/:account_id/courses?per_page=250

For a while now to get all courses within a specific Canvas account. It has worked great, up to 100 courses. Now today I have 102 courses, and the last 2 courses after 100 are not showing up. Per the API documentation, pagination is in place where I should expect Link headers to give me another URL to call to get the remaining courses. This isn't happening.

Here are the Link headers I'm getting from my initial API request:

[link] => ; rel="current",; rel="next",; rel="first",; rel="last"

There is no URL being provided for rel=next or any rel for that matter

I also ran this test: I added request parameter of published=true, unpublished one of my old courses, and the 101st course showed up. So the request is definitely being limited to 100 courses.

GET /api/v1/accounts/:account_id/courses?published=true&per_page=250

I have also tried this:

GET /api/v1/accounts/:account_id/courses?per_page=10

Even when I set pagination to 10 results only, the Link headers are all still blank (no URLs provided) even though I have 100+ published courses. My results only bring back 10 courses.

Has anyone experienced this before? Coding in PHP on a wordpress site.

Sample request I'm doing:

 $get_courses = wp_remote_get('https://myuniversity.instructure.com/api/v1/accounts/6/courses?per_page=250', array(
'headers' => array(
'Authorization' => 'Bearer ' . $authToken
),
));
 print_r($get_courses);

The above returns the headers, body etc with the Link headers being blank and only showing 100/102 courses

Labels (1)
1 Solution
ek
Community Member

UPDATE:

Was finally able to retrieve the next URL using the below code. Targeted the Link header and then specifically grabbed the URL between the "<>" characters. Thanks everyone for your comments! Specific thanks to James Jones in this thread: Handling Pagination. Hope this helps someone in the future.

     $linkHdr = wp_remote_retrieve_header($get_webinars, 'link');
     $Hdrlinks = explode(',', $linkHdr);
     $nextpage = NULL;

     foreach ($Hdrlinks as $link) {
         if (preg_match( '/<(.*?)>; rel="next"$/', $link, $matches )) { 
           $nextpage = $matches[1];
           echo "nextpage<br />";
           echo $nextpage;
         } 
     }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post