使用.Net HttpClient()

时间:2017-11-10 16:41:28

标签: c# curl dotnet-httpclient calendly

我正在尝试使用.net HttpClient()为Calendly创建一个webhook订阅

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

我正在尝试将此Curl命令转换为.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

这是我的.Net代码:

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);
}

我收到此错误422错误,但无法确定要更改的内容以使其正常工作。

获取错误无法处理的实体

{&#34; type&#34;:&#34; validation_error&#34;,&#34; message&#34;:&#34;验证失败&#34;,&#34;错误&#34;: {&#34;活动&#34;:[&#34;不能为空白&#34;]}}

我可以运行Curl命令,它可以在同一台机器上正常工作,所以我知道这是有效的。

我创建了一个.net HttpClient调用来测试基本令牌,它运行正常。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

最后回来了,我一看到代码就把它想出来了。

最初我把整个网址视为一个大字符串,但最初没有意识到&amp;将值分开以通过的符号。

下面的错误代码:

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

应该更改为从文档中分离每个值:

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")
};

因此,对于那些希望使用.Net来创建带有Calendly的webhook订阅的人来说,这是一个完整的TestMethod代码来试用它。只需用requestb.in或post url替换第一个参数即可。还要加入你的Calendly api密钥。

    [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);
    }

希望这有助于某人!