Community Novice

Problem when POSTing (C# ASP.NET)

I'm trying to use the Canvas API through C# to create some courses

I set the Authorization header to Bearer ########API KEY########

Content-Type: application/json

Encoding is UTF-8

Request Body:

{"sis_course_id":"AC1001 16/17 S1","name":"Introduction to Archaeology: 16/17 (S1)","course_code":"AC1001","workflow_state":"unpublished","enrollment_term_id":67}

And POST to

It comes back with a 200, and the JSON of a new course.

However, the new course is always 'Unnamed' and doesn't reflect any of the JSON I've sent.

Any ideas?  I'm pretty stumped as it's not throwing any error messages.


  using (WebClient client = new WebClient())


                client.Encoding = Encoding.UTF8;

                client.Headers[HttpRequestHeader.Authorization] = "Bearer " + token;

                client.Headers[HttpRequestHeader.ContentType] = "application/json";

  string data = "{"sis_course_id":"AC1001 16/17 S1","name":"Introduction to Archaeology: 16/17  S1)","course_code":"AC1001","workflow_state":"unpublished","enrollment_term_id":67}"

                string response = client.UploadString(rootUrl + "/api/v1/accounts/1/courses", "POST", data);


Thanks for any help!

Tags (3)
0 Kudos
3 Replies
Community Novice

Chris, I'm almost hesitant to post this response as I'm so new to this, and I typically work with VB.NET so I'm not a C# expert, and so far I've only been using the API with form parameters, not by sending JSON string... anyway about the only thing I can see that's different than what I would have done here is that although the attribute names in your JSON string are compliant with the JSON response model described in the API documentation, I've found that there seems to be a variation in these attribute names when POSTing the data using form parameters...  for example, the JSON attribute "name" corresponds to the POST form parameter course[name].  So, I'm not sure if that would have any impact when you're sending a JSON string.  I'm just providing this input in case it points you in a useful direction.


0 Kudos

Thanks for your response, it's always worth chucking your eggs in the basket!

Would you be able to post any VB.NET code example that does a POST / PUT that works?

I'll try falling back to using form parameters tomorrow if I still can't get JSON to work.

0 Kudos

Certainly.  This specific code is for creating a user (I just haven't gotten around to the course record yet), but I suspect the general idea is the same for the course. Note that in the code I reference an object named JSON.user  that's a custom class I've set up to receive the JSON response from the API. I can post that too if you wish.   Here is what I've used to create a user:

Public Function Create() As JSON.user

  If IsValid() Then

  Dim PostURL As String = URLBase + "/api/v1/accounts/1/users"

  Dim u As JSON.user

  Using client As New Net.WebClient

  client.Headers.Add("Authorization", "Bearer " + Token)

  Dim HTTPResponse As Byte() = client.UploadValues(PostURL, "POST", GetFormParameters())

  Dim HTTPResponseString As String = (New Text.UTF8Encoding).GetString(HTTPResponse)

  u = JsonConvert.DeserializeObject(Of JSON.user)(HTTPResponseString)

  End Using

  Return u


  Return Nothing

  End If

End Function

Private Function GetFormParameters() As Specialized.NameValueCollection

  Dim FormParameters As New Specialized.NameValueCollection

  FormParameters.Add("pseudonym[sis_user_id]", UserID)

  FormParameters.Add("pseudonym[unique_id]", LoginID)

  FormParameters.Add("user[name]", FirstName + " " + LastName)

  FormParameters.Add("user[short_name]", PreferredName + " " + LastName)

  FormParameters.Add("user[sortable_name]", LastName + ", " + FirstName)

  FormParameters.Add("communication_channel[type]", "email")

  FormParameters.Add("communication_channel[address]", Email)

  Return FormParameters

End Function

0 Kudos