Send a POST request to create an object which it exists already in canvas

RezaMazroei
Community Member
2
1338

This is a general question about APIs. Imagine, we have an object (users, assignments, etc) in canvas with all parameters set especially (SIS ID, or integration ID). Now, I want to know, if I send an API(POST request) with exact parameters to canvas, what would happen when the object exists already in canvas?

The canvas ignore the POST request? or overwrite the object with the new request, or create a duplicate?

I am asking this question because I am developing a python code, reads the data from the database and sends POST requests to create an object in canvas, so I am worried if I send a request which the object is already into the system.

2 Comments
James
Community Champion

@RezaMazroei 

Check for the existence of the record first and then use a PUT to update rather than a POST to create. Do it the right way rather than trying to figure out the behavior for the wrong method. Using a POST when you should be using a PUT will typically not give you what you want.

The longer answer is that it depends on which API endpoint you're using. Some values cannot be duplicated and you will get an error if you try. Others will gladly allow you to send the same POST command multiple times and will generate a new object each time.

For example, if I try to create a user with the same unique_id, throws an error. If I try to create a user with a different unique_id but with a previously used sis_id, it throws an error.

For other endpoints, like creating a content page, it creates a new page and appends a number at the end "Test 1", "Test 1-2", "Test 1-3", etc. You need to use PUT to update an existing page or it creates a new one.

A second suggestion is to use the beta instance of Canvas so you can test things without messing up the production site.

Seriously, though, make sure you're using the right method POST or PUT as documented and then you don't have the issue.

maguire
Community Champion

I use the method that @James describes for course calendar entries for thesis presentations and announcements for these presentations, I look to see if it is the same title and then the same student or description - if so, just do an update. 

See JSON_to_calendar.py at https://github.com/gqmaguirejr/E-learning 

(it should probably not check for the time being the same, then it would allow you to change the starting time.)