首页 > 解决方案 > C# .NET 使用 Trello REST API 更新卡片上的自定义字段项

问题描述

我正在使用 C# 中的 .NET 创建一个应用程序以在 Trello 板上创建卡片,但我无法对自定义字段进行更新。我已经搜索了 API 文档和互联网,但无法使其正常工作。

我用来更新卡片上的自定义字段值的代码如下:

private static async Task<String> PutCustomFieldsAsync(string Url, string jsonContent)
{
    using (var httpClient = new HttpClient())
    {
        var response = await httpClient.PutAsync(Url, new StringContent(jsonContent, System.Text.Encoding.UTF8, "application/json"));
        json = await response.Content.ReadAsStringAsync();
    };

    return json;
}

变量的值如下:

网址:“https://api.trello.com/1/cards/6...3fe/customField/5...bb9/item?key=4...f&token=e...6”

jsonContent: "{"Value":{"text":"test"}}"

当我在 Trello 上手动设置字段然后获取卡自定义字段时,出现的 JSON 具有以下数据:

{
    "id": "6...c66",
    "value": {
      "text": "teste"
    },
    "idCustomField": "5...bb9",
    "idModel": "6...3fe",
    "modelType": "card"
  }

由于“自定义字段类型的值无效”,我得到的响应是“400 Bad Request”。

任何人都可以帮我解决这个问题吗?

谢谢。

标签: c#.nettrello

解决方案


如果有人遇到同样的问题,我设法让 trello 使用以下代码更新自定义字段:

using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Collections;

private static async Task<String> PutCustomFieldsAsync(string CardId, string CustomFieldId, string AppKey, string Token, string Value)
{
    string json = null;

    Hashtable tableAux= new Hashtable();
    tableAux["text"] = value;
    Hashtable table = new Hashtable();
    table["value"] = tableAux; 

    string Url = String.Format("https://api.trello.com/1/cards/{0}/customField/{1}/item?key={2}&token={3}", CardId, CustomFieldId, AppKey, Token);

    string jsonContent= JsonSerializer.Serialize(table);

            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod(Method), Url))
                {
                    request.Headers.TryAddWithoutValidation("Content - Type", "application/json");
                    request.Content = new StringContent(jsonContent);
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                    HttpResponseMessage response = httpClient.SendAsync(request).Result;
                    if (response.IsSuccessStatusCode)
                        json = await response.Content.ReadAsStringAsync();
                }
            }
    return json; 
}

推荐阅读