Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
Found this content helpful? Log in or sign up to leave a like!
Hello everybody,
We are using .NET C# to create an API client wrapper to automate some things. I've gotten it to work very well for many GET requests so far, but for some reason a POST to create a course is returning a 404 and I don't know why.
I could post my code but not sure if that would benefit this discussion that much. Let me know if it helps to provide the code I'm using.
I'm using the path URI provided by the API documentations: {our canvas url}/api/v1/accounts/1/courses
I'm passing the following form fields to an HttpClient object calling PostAsync():
course[name]
course[start_at]
course[end_at]
course[code]
course[license]
course[is_public]
course[public_description]
course[term_id]
course[sis_course_id]
course[time_zone]
course[post_manually]
enable_sis_reactivation
And no matter what values I'm passing I'm getting the 404 response. I find this interesting because the GET works and it uses the same URI. Also a team member is using Python to create a course manually as an experiment and it works for him.
I'm sorry I don't have much else to go by. And this is my first foray into interfacing with Canvas so with a general lack of experience prior to a few weeks ago I'm a "newbie" with Canvas. Most of my previous API work has been with other platforms and for LMS's it's been Moodle where I have not had any issues.
I decided I will throw my code in here too, in case there's something I've overlooked prior to making the POST call.
public static bool CreateTermCourse(string CourseCode, DateTime TermBeginDate, string Time, int Session, string ExternalID)
{
// fetch Course information from SMS first
DataRow rowCourseInfo = DB.GetCourseInfoForCanvas(CourseCode, TermBeginDate, Time, Session);
if (rowCourseInfo == null)
{
return false;
}
// create a post
HttpClient client = GetHttpClient();
// create form value/pair data
Dictionary<string,string> values = new Dictionary<string,string>();
values.Add("course[name]", rowCourseInfo["CourseName"].ToString());
values.Add("course[start_at]", Convert.ToDateTime(rowCourseInfo["TermBeginDate"]).ToString("yyyy-MM-ddT00:00:00Z"));
values.Add("course[end_at]", Convert.ToDateTime(rowCourseInfo["TermEndDate"]).ToString("yyyy-MM-ddT00:00:00Z"));
values.Add("course[code]", CourseCode.Replace(" ", ""));
values.Add("course[license]", "private");
values.Add("course[is_public]", "false");
values.Add("course[public_description]", rowCourseInfo["CourseDescription"].ToString());
values.Add("course[term_id]", rowCourseInfo["TermID"].ToString());
values.Add("course[sis_course_id]", ExternalID);
values.Add("course[time_zone]", "America/Los_Angeles");
values.Add("course[post_manually]", "false");
values.Add("enable_sis_reactivation", "true");
// encode the form data
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
// post it
Task<HttpResponseMessage> message = client.PostAsync("https://{Our Canvas URL}/api/v1/accounts/1/courses", (HttpContent)content);
// evaluate the response
if (message.Result.StatusCode != HttpStatusCode.OK)
{
return false;
}
// handle response
string sResult = message.Result.Content.ReadAsStringAsync().Result;
//JsonContent
Dictionary<string, string> keyValuePairs = (Dictionary<string, string>)JsonSerializer.Deserialize(sResult, typeof(Dictionary<string, string>));
// Extract the Canvas ID number to insert back into SIS
Int64 iCanvasCourseID = Convert.ToInt64(keyValuePairs["id"]);
// Update SIS with the ExternalID
DB.SetCanvasCourseIDFromExternalID(ExternalID, iCanvasCourseID);
return true;
}
Have you verified that the account ID is correct?
I was going to ask if you'd checked that the role which owns the token you are using has permissions to do what you want.
Yes I have the appropriate role. I'll copy what I answered above in case you don't get those notifications.
Sorry for the late response. Been submerged in a nasty existing LMS issue for a week now. I think I figured out the problem though, and it pertains to the callout of the names in the name/value pairs being passed, as well as the format. I assumed that it was form data, but it's not, it's body data with JSON. I'll be back at this again to carry on where I left off once I'm no longer submerged 10000 leagues beneath the sea! 🙂
Yes the account ID is correct. Sorry for the late response. Been submerged in a nasty existing LMS issue for a week now. I think I figured out the problem though, and it pertains to the callout of the names in the name/value pairs being passed, as well as the format. I assumed that it was form data, but it's not, it's body data with JSON. I'll be back at this again to carry on where I left off once I'm no longer submerged 10000 leagues beneath the sea! 🙂
To participate in the Instructure Community, you need to sign up or log in:
Sign In