.NET C# client returning a 404 on a POST request to create a course
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
}