How to Edit an Assignment with the API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I want to edit an assignment. I'm working in Node Express and using the Request NPM package to send HTTP requests. I have an API key and I can successfully make a number of calls that request information about modules and assignments. But when it comes to updating an assignment I'm not having much luck. I don't get an error, but I get a long HTML file in the "body" parameter returned by canvas stating that I'm not logged in or that my login has timed out.
What am I doing wrong? Where can I find examples of how to make this kind of call? I've seen the scanty API documentation on this subject but would like a more detailed tutorial or example. Again, I can make a number of calls successfully, it is this particular call to edit an assignment that I want to learn more about.
If using the request npm package is not the best approach, I'm open to suggestions...
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is not requests specific advice. It's likely not the problem if it works for other things. The long HTML file normally happens when the request is malformed.
Things to check (no particular order)
- You have the /api/v1 in the URL
- You are sending the token in the header (not required, but good idea)
- You are using PUT instead of GET.
- If you are using JSON, then you include a content-type: application/json header
- You may also need to include a header of accept: application/json in some cases to get rid of the while(1); that Canvas adds. Sometimes it doesn't seem to be necessary, other times it does.
- You have a properly formed request.
The last one is probably the biggest issue people face. You cannot take an assignment object from a GET, change a few things, and send it back directly with a PUT. Look carefully at the documentation to edit an assignment.
There is a list of what it can accept and how it needs prefixed. Most of it needs inside of an assignment object. When you get the object from a GET, it's not inside the assignment object, but it needs wrapped before you send it back. It's assignment[name], not just name.
With a PUT, you should only need to send what has changed (or things listed as required). Don't try to send the entire object back, just the things you need to change.