Register for InstructureCon25 • Passes include access to all sessions, the expo hall, entertainment and networking events, meals, and extraterrestrial encounters.
Found this content helpful? Log in or sign up to leave a like!
Hello All,
I'm hoping someone can help me understand something that I can seem to get my head around. I'm trying to see a list of a users page views past the last 100 entries. I have the following query entered through the URL
/api/v1/users/xxx/page_views?per_page=100&page=y
So my thought was I'll just view it through my browser just to better understand the structure, the first page looks correct, so added 1 to the page number to see the next page, exactly the same results returned. Seemed unusual so I upped the page number to a ridiculously large number to ensure it would return an empty array. But it didn't, it returned the same 100 results again and continues to regardless of the page number I enter.
I've used this query structure before to return data and it would correctly return back the next page as needed. I've been pointed towards the rel value in terms of first, next and last, but that's not making sense to me. The rel values seem to relate more to a page count rather then data repeating.
Has anyone come across this before, any idea whats happening and what I'm not understanding?
Thanks
Although lots of the Canvas API endpoints use a 'page' parameter to step through the results, this isn't how the pagination documentation specifies you should use the API. You should be parsing the `Link` header and using that to move to the next page of results. In the case of the 'page_views' endpoint I'm getting back bookmark result for the next page, for example:
It's more work, but most languages have reasonable libraries for parsing the link header. I've successfully used https://www.npmjs.com/package/parse-link-header in some Javascript code for this.
+1 for parsing the link header. I use this process in Python to build a dictionary of paged results with requests:
response = requests.get(canvasUrl, headers=headers, params=params)
searchResults = response.json()
while 'next' in response.links:
response = requests.get(response.links['next']['url'], headers=headers, params=params)
searchResults.extend(response.json())
To interact with Panda Bot in the Instructure Community, you need to sign up or log in:
Sign In