【问题标题】:Setting custom field values on a card with the Trello API使用 Trello API 在卡片上设置自定义字段值
【发布时间】:2018-09-02 16:09:03
【问题描述】:

我正在尝试使用 Trello API 的新自定义字段方法来设置卡片上自定义字段的值。

我创建了一个number 类型的自定义字段。当我通过 id 对自定义字段发出 GET 请求时,它会返回此自定义字段对象:

{
"id":"5ab13cdb8acaabe576xxxxx",
"idModel":"54ccee71855b401132xxxxx",
"modelType":"board",
"fieldGroup":"837a643fd25acc835affc227xxxxxxxxxxxxxxxxxxxx",
"name":"Test Number Field",
"pos":16384,
"type":"number"
}

然后,当我在 Trello UI 中创建新卡片(但不要在 Test Number Field 框中输入值)然后使用 GET 使用 customFieldItems=true 的卡片(如文档中的 here),它返回此卡片对象(删除无关字段):

{
"id": "5ab56e6b62abac194d9xxxxx",
"name": "test1",
"customFieldItems": []
}

请注意,由于我没有在 UI 的 Test Number Field 框中输入任何内容,因此 customFieldItems 属性包含一个空数组。

然后,如果我在 UI 的 Test Number Field 框中输入数字 1 并再次输入 GET 卡片,它会返回此信息(已删除无关字段):

{
"id": "5ab56e6b62abac194d9xxxxx",
"name": "test1",
"customFieldItems":
    [
        {
        "id": "5ab570c5b43ed17b2dxxxxx",
        "value": {
            "number": "1"
            },
        "idCustomField": "5ab13cdb8acaabe5764xxxxx",
        "idModel": "5ab56e6b62abac194d9xxxxx",
        "modelType": "card"
        }
    ]
}

我希望能够通过 API 设置此自定义字段的值。

当我转到“设置、更新和删除卡片上自定义字段的值”的 API 文档时, (here) 我插入以下信息: p>

查询授权

  • key(我们有效/有效的 Trello API 密钥)

  • token: (我们的有效/工作 Trello API 令牌)

路径参数

  • idCard(应为其设置/更新自定义字段值的卡的 ID) 5ab56e6b62abac194d9xxxxx

  • idCustomField(卡片上自定义字段的 ID。)5ab570c5b43ed17b2dxxxxx

查询参数

  • idCustomField(项目所属的自定义字段的 ID。):5ab13cdb8acaabe576xxxxx

  • modelType (这应该始终是卡片。)card

  • value (包含要为卡的自定义字段值设置的键和值的对象。用于设置值的键应与定义的自定义字段的类型匹配。):@987654349 @

当我点击 Try It 时,我会收到回复:400 Bad Request "Invalid custom field item value."

我尝试了以下方法:

  • 切换idCustomField这两个值(路径参数和查询参数同名,意思是接受同一个值,但描述不同,描述模糊/令人困惑)。

  • 将两个 idCustomField 值设置为相同的值(对于两个可能的 ID)

  • 将值设置为2{"number": "2"}{number: 2}{number: "2"} 等。

无论我尝试什么,我总是得到"Invalid custom field item value." 无论卡片在自定义字段中是否有值,其行为方式都是相同的。

我很确定路径参数中的 idCustomField 被接受,因为当我更改一个字符时,它给了我这个错误:"invalid value for idCustomField"

所以我不知道"Invalid custom field item value." 是指查询参数idCustomField* 还是value

我也不知道卡片是否在自定义字段中具有现有值是否会有所不同,但我希望能够设置此自定义字段的值,无论它当前是否具有字段中的值。

【问题讨论】:

  • 我相信这是value 的问题,因为当我尝试https://api.trello.com/1/card/.../customField/.../item?value=&key=...&token=... 时,它成功清除了值。也许我们应该以不同于文档所说的方式对值对象进行编码或字符串化。我会尝试其他方法并让您知道。

标签: api object trello trello-powerup


【解决方案1】:

the Trello documentation page 上的实时示例(使用 XMLHttpRequest)是错误的。您应该使用fetch 的下一个示例。

var url = "https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?token={yourToken}&key={yourKey}";
var data = {value: { number: "42" }};
fetch(url, { body: JSON.stringify(data), method: 'PUT', headers: {'content-type': 'application/json'}})
.then((resp) => resp.json())
.then((data) => console.log(JSON.stringify(data, null, 2)))
.catch((err) => console.log(JSON.stringify(err, null, 2)))

这个例子有效。在尝试了这个之后,我修改了XMLHttpRequest 版本,它也可以工作。

var data = null;
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

data = {value: { number: "3" }};  //added
var json = JSON.stringify(data);  //added

xhr.open("PUT", 'https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?key={yourkey}&token={yourtoken}');
xhr.setRequestHeader('Content-type','application/json');  //added
xhr.send(json);  //modified

重点是您应该 1) 将请求 Content-type 标头设置为 application/json 和 2) 通过 JSON 对象主体传递 value

我尝试编辑文档中的实时示例,但我无法做到。我希望他们能尽快修复它。

【讨论】:

  • 谢谢,成功了!此外,看起来 Trello 刚刚更新了他们的文档页面以删除无关的查询参数 idCustomFieldmodelType,并且他们还添加了一个注释以始终将字符串作为值发送(例如布尔值和数字值)。
  • @Employee:“总是将字符串作为值发送(如布尔值和数字值)”救了我。感谢您指出这一点!
  • 这段代码可以简化为调用的url吗?我在 R 中遇到同样的问题,但无法翻译它
猜你喜欢
  • 2020-10-18
  • 2021-09-12
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 2021-01-01
  • 2016-12-30
  • 2021-09-04
  • 2014-10-30
相关资源
最近更新 更多