【问题标题】:How do I set a custom field in Trello using C# and HttpRequestMessage?如何使用 C# 和 HttpRequestMessage 在 Trello 中设置自定义字段?
【发布时间】:2021-01-01 18:09:43
【问题描述】:

我可以使用此代码请求董事会的卡片和自定义字段:


  string qString = @"https://api.trello.com/1/boards/{boardId}/cards?customFieldItems=true&key={myKey}&token={myToken}";
  using(var request = new HttpRequestMessage(new HttpMethod("GET"), qString))
  {
    var response = await httpClient.SendAsync(request);
    String JsonString = await response.Content.ReadAsStringAsync();

它会返回有关卡片的信息,包括以下内容,这些信息说明了我的自定义字段项:

   {
    "id": "5f5432b72678573fd5e#####",
     "value": {
       "text": "AAA"
     },
     "idCustomField": "5d24ae4cfe7cb10d611#####",
     "idModel": "5f542e224c5d2c2679d#####",
     "modelType": "card"
   }

现在我正在尝试使用以下代码设置值:

    using(var httpClient = new HttpClient())
    {
       using(var request = new HttpRequestMessage(new HttpMethod("PUT"), @"https://api.trello.com/1/cards/" + myCardId + "/" + myCustomFieldId + "/item?" + "key={myKey}&token={myToken}" + "{value: { text: \"New Value\"}}"))
       {
            // i don't know how to set the header content so i commented it out
            //request.Headers.Add("content-type", "application/json");
            var response = await httpClient.SendAsync(request);
            String JsonString = await response.Content.ReadAsStringAsync();

但它回来说它不能 PUT...

谢谢, 东风

【问题讨论】:

  • 你能发布错误信息吗?
  • JsonString = 无法放置 /1/cards/{cardId}/{id}/item?key={key}&token={{token}%7Bvalue:%20%7B%20text:%20 %22帮助%22%7D%7D
  • response = {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { X-Dns-Prefetch-Control: off X- Frame-Options: DENY X-Download-Options: noopen X-Permitted-Cross-Domain-Policies: none Referrer-Polic...
  • 无法 PUT /1/cards/{cardId}/{CustomFieldId}/item?key={myKey}&token={myToken}%7Bvalue:%20%7Btext:%20%22TestName%22% 7D%7D
  • 当我包含标头的注释行以便我可以指定它是 application/json 时出现的错误是:System.InvalidOperationException: 'Misused header name。确保请求标头与 HttpRequestMessage 一起使用,响应标头与 HttpResponseMessage 一起使用,内容标头与 HttpContent 对象一起使用。'

标签: c# trello


【解决方案1】:

感谢您的帮助。

我能够使用这个将 Curl 转换为 C# 的网站找出我的问题:

https://curl.olsh.me/

private async void button4_Click(object sender, EventArgs e)
{
   using(var httpClient = new HttpClient())
   {
       String query = @"https://api.trello.com/1/card/" + myCardId + "/customField/" + myCardsCustomFieldItems_IdCustomField + "/item";
     using(var request = new HttpRequestMessage(new HttpMethod("PUT"), query))
     {
        request.Content = new StringContent("{  \"value\": { \"text\": \"Hello, world!\" }, \"key\": \"########\",  \"token\": \"#######\" }");
        request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");

        var response = await httpClient.SendAsync(request);
        String JsonString = await response.Content.ReadAsStringAsync();

        this.textBox4.Text = query;
        this.richTextBox4.Text = JsonString;
      } 
    }
  }

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2018-09-02
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    相关资源
    最近更新 更多