Found this content helpful? Log in or sign up to leave a like!

Canvas API adds "-#" to the end of the URL when creating course page.

Jump to solution
vihdutta
Community Member

I'm using the Canvas API to create pages and link these created pages dynamically between other pages. If the page has been created before in the course, regardless of whether the page has been deleted, the page slug (ex: the "week-1" in "https://xxx.instructure.com/courses/799908/pages/week-1") adds a hyphen and number to the end of the slug where the number increments depending on how many times the same page name has been created before.

This behavior is not ideal when trying to "predict" the page slug for automatically generated links. Could someone help navigate this issue? Below is similar to the code I'm using to upload pages.


 headers = {"Authorization": f"Bearer {access_token}"}
    slug = re.sub(r"[^a-z0-9]+", "-", title.lower()).strip("-")
    data = {
        "wiki_page[title]": title,
        "wiki_page[body]": html_content,
        "wiki_page[published]": True,
        "on_duplicate": "overwrite",
    }

        data["wiki_page[page_url]"] = slug
        resp = requests.post(
            f"https://xxx.instructure.com/api/v1/courses/{course_id}/pages",
            headers=headers,
            data=data,

    resp.raise_for_status()
    return resp.json()
Labels (3)
0 Likes
1 Solution
chriscas
Community Coach
Community Coach

Hi @vihdutta,

Honestly I wouldn't try to "predict" page URLs at all, as you have seen it's almost impossible to do.  What I'd perhaps do instead is create all of your items first so you can get the actual URLs Canvas generated saved somewhere, then edit those items and use the URLs you saved.  This may be additional code, but it'll probably make a more stable end product.

-Chris

View solution in original post

0 Likes