cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
aday
Community Participant

Update Course Name with API in c#

Jump to solution

I am having a issue trying to update a course name in c#. I perform the PUT and I get a 200 OK back but the name does not change.

Here are the details:

URL: /api/v1/courses/1724

Content: "{\"id\":1724,\"name\":\"2 Test - 1 Test - Competing in and with China\"}":  - this is the data that is used to initialize the StringContent Object

   string jsonData = JsonConvert.SerializeObject(data);    HttpContent content = new StringContent(jsonData);

    var response = (HttpResponseMessage)Task.Run(async () => await client.PutAsync(urlCommandcontent)).Result;

This returns a 'StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1' but the data is never updated

Does anyone have any ideas??

Labels (1)
Tags (5)
0 Kudos
1 Solution

Accepted Solutions
James
Community Champion

 @aday ,

 

I can't help with C#, but your parameters are improperly formed. Canvas is accepting the call, ignoring anything it doesn't recognize (which is the entire payload), saying "OK" to the empty list of changes, and not making any changes.

The Update a course endpoint requires that all of the parameters be wrapped inside the course property.

ID is not supported as a parameter, but it is specified in the path portion of the URL. Do not include it in your content as it will be ignored.

The course name is course[name] as a query parameter. In JSON, it would look like: { "course" : { "name" : "2 Test - 1 Test - Competing in and with China" } }

View solution in original post

3 Replies
James
Community Champion

 @aday ,

 

I can't help with C#, but your parameters are improperly formed. Canvas is accepting the call, ignoring anything it doesn't recognize (which is the entire payload), saying "OK" to the empty list of changes, and not making any changes.

The Update a course endpoint requires that all of the parameters be wrapped inside the course property.

ID is not supported as a parameter, but it is specified in the path portion of the URL. Do not include it in your content as it will be ignored.

The course name is course[name] as a query parameter. In JSON, it would look like: { "course" : { "name" : "2 Test - 1 Test - Competing in and with China" } }

View solution in original post

aday
Community Participant

Thanks I had started down the route also but was still getting the same issues. Turns out i needed to set the encoding and media type.

Snippet

HttpContent content = new StringContent(jsonDataEncoding.UTF8, "application/json");
jsimon3
Community Participant

this might help in the near future:

using static Newtonsoft.Json.Formatting;

Newtonsoft.Json Namespace