Problem Posting C# (Calendar Event) JSON

Jump to solution
Community Explorer

I'm trying to post a calendar event using C# HttpWebRequest and am running into issues.  I've borrowed from a previous similar project but am presently stumped.  I'd be very grateful for any suggestions!

Here is the code chunk:

private int sendNewEvent(string JSON)

string token = Canvas_Connect.Token;
string requestURL = Canvas_Connect.CanvasBaseURL + "calendar_events";  

//requestURL is  ""

var httpWebRequest = (HttpWebRequest)WebRequest.Create(requestURL);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", "Bearer " + token);

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
streamWriter.Write(JSON);  //see end of code for value when running

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
var result = streamReader.ReadToEnd();

CalendarEvent CalResp = JsonConvert.DeserializeObject<CalendarEvent>(result);
txtResponse.Text = result;

catch (WebException ex)
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();

//just looking at this for debugging now...
return -1;


When I run this, the response I get is this:

{"errors":[{"message":"The specified resource does not exist."}]}

JSON String value: 

{"context_code":"user_97","title":"Lojong Buddhist Teachings, Chesebro 114, Mr. Eaton","description":"Lojong Buddhist Teachings, Chesebro 114, Mr. Eaton","start_at":"2021-05-27T08:30:00-04:00","end_at":"2021-05-27T09:30:00-04:00","location_name":null,"all_day":false}

I'd be grateful for any pointers! 


Labels (4)
1 Solution
Community Champion


I cannot help with the C# part of it, but your JSON is not correct. Note that I did not check the JSON itself, but there is a missing layer. When you look at the documentation for creating a calendar event, there is a calender_event property that holds all of the attributes (context_code, title, description, etc).

{ calender_event: everything you currently have }

View solution in original post