Can return enrollments- how to handle pagination?

salmonsm
Community Novice

This Node script is working well to return enrollments- but I am struggling with pagination.

const request = require('request');



let url = 'https://mysite.instructure.com:443/api/v1/courses/123456/enrollments?type[]=StudentEnrollment&state[... rel="next"';

request(url, function (err, response, body) {
if(err){
console.log('error:', error);
} else {
let results = JSON.parse(body)

for (var i in results) {
console.log(results[i]);
}
}

});

the only way i know how to cycle through the pages is manually with page number in the URL, example: page=2, page=3, etc.

How can this be automated?

How can I design the script so it knows it's at the end? I assume an if-then structure that checks the state of rel. As long as it's "next," it keeps going, but what about when it's "last?"

Any help would be most appreciated. It's a rather urgent need. Thank you.

Labels (1)