cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
kevineaton
Community Member

Problem Posting C# (Calendar Event) JSON

Jump to solution

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)
{

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

//requestURL is  "https://hunschool.instructure.com/api/v1/calendar_events"

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
streamWriter.Flush();
streamWriter.Close();
}

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;
return CalResp.id;
}

}
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)
0 Kudos
1 Solution

Accepted Solutions
James
Community Champion

@kevineaton 

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

3 Replies
James
Community Champion

@kevineaton 

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

kevineaton
Community Member

Thank you! This really threw me off: https://canvas.instructure.com/doc/api/calendar_events.html#CalendarEvent. I would have thought it would have been more clearly represented in that spec.

 

At any rate, thanks for taking the time to respond. I was spinning my wheels and it works now!

James
Community Champion

@kevineaton 

The top part of the documentation explains what an object looks like, but the rest of the page explains the way the API calls must be formatted. GET operations typically return the objects from the top while POST and PUT operations often involve a wrapper around the object.