Edit Assignment with API - Error: assignment is missing

Jump to solution
bmorris24
Community Member

Hi all,

I'm new to the Canvas API and am doing some rudimentary testing. I am currently trying to edit an assignment through the API, and the response I get is 400: assignment is missing. It seems very similar to this post except that the provided solution is not applicable to me (so far as I can tell). Here is my code:

header = {'Authorization': f'Bearer {access_token}'}  # Access token previously set
course_id = 123
assignment_id = 4567
base_url = f'https://canvas.instructure.com/api/v1/'

course_url = f'{base_url}/courses/{course_id}'
course_response = requests.get(course_url, headers=header)
print(course_response.json()) # As expected

assignment_url = f'{base_url}/courses/{course_id}/assignments/{assignment_id}'
assignment_response = requests.get(assignment_url, headers=header)
print(assignment_response.json()) # Also as expected

new_duedate = datetime(2023, 7, 28).isoformat()
edit_response = requests.put(assignment_url, params={'due_at': new_duedate}, headers=header)
print(edit_response.json()) # Response 400: assignment is missing
 

Getting the course and getting the assignment both run smoothly. This assignment was created through an API call, which also ran smoothly (I double checked on the actual course page). It is just editing the assignment that causes trouble. Any thoughts would be greatly appreciated!

 

EDIT: I have tried switching params= to both data= and json=, none of which have worked (same error).

Labels (3)
0 Likes
1 Solution
DecoyLex
Community Participant

I took another look at your code and the API docs and was able to get the following to work:

edit_response = requests.put(assignment_url, json={'assignment': {'due_at': new_duedate}}, headers=header)

 

I made this change based on the parameter names on the Edit an assignment documentation. Since the parameter names were in the form of assignment[due_at], I tried wrapping the fields in an assignment object.

View solution in original post

0 Likes