Search code examples
c#curldotnet-httpclientcalendly

Error creating Calendly webhook subscription with .Net HttpClient()


I am trying to Create a webhook subscription with .net HttpClient() for Calendly

https://developer.calendly.com/docs/webhook-subscriptions

I am attempting to convert this Curl command to .Net

curl --header "X-TOKEN: <your_token>" --data "url=https://blah.foo/bar&events[]=invitee.created&events[]=invitee.canceled" https://calendly.com/api/v1/hooks

Here is my .Net code:

private static async Task<HttpResponseMessage> PostCreateWebhookSubscription()
{
    var client = new HttpClient {BaseAddress = new Uri("https://calendly.com")};
    var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/hooks/");
    var keyValues = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("url",
            "https://requestb.in/17ruxqh1&events[]=invitee.created&events[]=invitee.canceled")
    };
    request.Content = new FormUrlEncodedContent(keyValues);
    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") {CharSet = "UTF-8"};
    request.Content.Headers.Add("X-TOKEN", "<my_calendly_token>");
    return await client.SendAsync(request);
}

I get this error 422 error, but unable to figure out what to change to make this work.

getting error Unprocessable Entity

{"type":"validation_error","message":"Validation failed","errors":{"events":["can't be blank"]}}

I am able to run the Curl command and it works fine from the same machine, so I know that is working.

I created a .net HttpClient call to test the basic Token, which worked fine.

Any suggestions?


Solution

  • Finally came back this which figured it out as soon as I looked at the code again.

    Originally I was looking at the whole URL as one big string, but did not realize at first the & symbol which was separating the values to pass.

    The bad code below:

    var keyValues = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("url",
            "https://requestb.in/17ruxqh1&events[]=invitee.created&events[]=invitee.canceled")
    };
    

    Should have been changed to this seperating each value from their documentation:

    var keyValues = new List<KeyValuePair<string, string>>
    {
         new KeyValuePair<string, string>("url","https://requestb.in/17ruxqh1"),
         new KeyValuePair<string, string>("events[]","invitee.created"),
         new KeyValuePair<string, string>("events[]","invitee.canceled")
    };
    

    So for those that want to use .Net to create webhook subscriptions with Calendly here is a full TestMethod code to try it out. Just replace the first parameter with your requestb.in or post url. Also put in your Calendly api key.

        [TestMethod]
        public void CreateCalendlyWebhookSubscription()
        {
            var task = PostCreateWebhookSubscription();
            task.Wait();
            var response = task.Result;
            var body = response.Content.ReadAsStringAsync().Result;
        }
    
        private static async Task<HttpResponseMessage> PostCreateWebhookSubscription()
        {
            var client = new HttpClient {BaseAddress = new Uri("https://calendly.com")};
            var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/hooks/");
            var keyValues = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("url","https://requestb.in/17ruxqh1"),
                new KeyValuePair<string, string>("events[]","invitee.created"),
                new KeyValuePair<string, string>("events[]","invitee.canceled")
            };
            request.Content = new FormUrlEncodedContent(keyValues);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") {CharSet = "UTF-8"};
            request.Content.Headers.Add("X-TOKEN", "<your Calendly ApiKey>");
            return await client.SendAsync(request);
        }
    

    Hope this helps somebody!