API Delete Enrollment using Python Requests?

Jump to solution
millerjm
Community Champion

I'm trying to write a script to remove me from courses that I get enrolled in as an admin when I create a new course.  I'm trying to use the Python Requests to delete the enrollment.  When I do it through my python script, it only concludes the enrollment (so it stays on my list) instead of deleting it.  At least then I'm not getting notifications or visible to students.  

json_headers = {'Authorization' : 'Bearer ' + api_token}


def api_unenroll_admin_course(course_id, enrollment_id):
    enroll_url = url + "/courses/" + str(course_id) + "/enrollments/" + str(enrollment_id)
    print(enroll_url)
    payload = {'enrollment[task]' : 'delete'}
    print(payload)
    r_delete = requests.delete(enroll_url, headers=json_headers, data=payload)
    print(r_delete.text)
    print(r_delete.url)
    print(r_delete)
‍‍‍‍‍‍‍‍‍

when I do it using PostMan as shown below, it actually deletes the enrollment (but I have to go look up each enrollment ID and then go run each delete call).  

275180_pastedImage_1.png

Does anyone have ideas about what I am doing wrong?  I think I'm leaving something out of the python code but not sure what.  

Thanks!

Joni

1 Solution
MattHanes
Community Champion

I didn't test this but it appears that your payload would need to be changed to 

payload = {'task' : 'delete'}

That would match your postman script. I think the server is ignoring your payload because it isn't formatted correctly and if the "task" argument that it's expecting isn't given, it concludes the enrollment instead of deleting.
275188_pastedImage_1.png

Hope that helps!

View solution in original post