You need to enable JavaScript to run this app. 使用R语言httr库调用Trello API更新自定义字段未生效
优惠活动
大模型
产品
解决方案
定价
更多

使用R语言httr库调用Trello API更新自定义字段未生效

R语言调用Trello API更新卡片自定义字段失效问题解决方案

问题现象

  • 基于测试账号凭证开发的R脚本,两次调用PUT接口更新卡片自定义字段分别返回404、400状态码,字段值无任何变更
  • 第一次请求尝试清空原有值(原有值为please change me from API),返回404,字段未清空
  • 第二次请求尝试将字段值改为something,返回400,字段仍保留原有内容

原始测试代码

require("httr")
PUT("https://api.trello.com/1/cards/5f6888ab6301b68b8a614156/customField/5f6889e2d2536e5364eed893/item?key=d674c81261ea78489bcef67dda228790&token=bdcdd065a63b82fb0e2f5f98a7464791698e4fa98004ec02f2690bce9ce2a59d&value=")
content(GET("https://api.trello.com/1/boards/5f6888aa5f8b800c21e81f6e?key=d674c81261ea78489bcef67dda228790&token=bdcdd065a63b82fb0e2f5f98a7464791698e4fa98004ec02f2690bce9ce2a59d&cards=all&card_fields=all&customFields=true&card_customFieldItems=true"))[["cards"]][[1]][["customFieldItems"]][[1]][["value"]][["text"]]

PUT("https://api.trello.com/1/cards/5f6888ab6301b68b8a614156/customField/5f6889e2d2536e5364eed893/item?key=d674c81261ea78489bcef67dda228790&token=bdcdd065a63b82fb0e2f5f98a7464791698e4fa98004ec02f2690bce9ce2a59d&value=something")
content(GET("https://api.trello.com/1/boards/5f6888aa5f8b800c21e81f6e?key=d674c81261ea78489bcef67dda228790&token=bdcdd065a63b82fb0e2f5f98a7464791698e4fa98004ec02f2690bce9ce2a59d&cards=all&card_fields=all&customFields=true&card_customFieldItems=true"))[["cards"]][[1]][["customFieldItems"]][[1]][["value"]][["text"]]

问题根因

  • 参数结构不符合接口要求:Trello自定义字段更新接口不接受URL查询字符串里直接传value参数,且值需要按照字段类型嵌套为JSON结构,不能直接传裸字符串
  • 清空值逻辑错误:清空自定义字段不是传空字符串,而是需要把value参数设为null
  • 硬拼URL易触发编码问题:直接把所有参数拼在URL字符串中,没有经过自动编码,很容易因为特殊字符、格式问题触发400错误

可运行修正代码

# 加载依赖
library(httr)

# 固定参数配置
api_key <- "d674c81261ea78489bcef67dda228790"
api_token <- "bdcdd065a63b82fb0e2f5f98a7464791698e4fa98004ec02f2690bce9ce2a59d"
card_id <- "5f6888ab6301b68b8a614156"
custom_field_id <- "5f6889e2d2536e5364eed893"
board_id <- "5f6888aa5f8b800c21e81f6e"
api_base <- "https://api.trello.com/1"

# 设置文本类型自定义字段值
set_resp <- PUT(
  url = paste0(api_base, "/cards/", card_id, "/customField/", custom_field_id, "/item"),
  query = list(key = api_key, token = api_token),
  body = list(value = list(text = "something")),
  encode = "json"
)
# 请求成功返回200状态码
status_code(set_resp)

# 验证字段更新结果
check_resp <- GET(
  url = paste0(api_base, "/boards/", board_id),
  query = list(
    key = api_key,
    token = api_token,
    cards = "all",
    card_fields = "all",
    customFields = "true",
    card_customFieldItems = "true"
  )
)
current_field_value <- content(check_resp)[["cards"]][[1]][["customFieldItems"]][[1]][["value"]][["text"]]

# 清空自定义字段值
clear_resp <- PUT(
  url = paste0(api_base, "/cards/", card_id, "/customField/", custom_field_id, "/item"),
  query = list(key = api_key, token = api_token),
  body = list(value = NULL),
  encode = "json"
)
status_code(clear_resp)

通用注意事项

  • 不同类型的自定义字段需要匹配对应的value嵌套结构:数字类型使用list(number = "数值字符串"),下拉选项类型使用list(option = "对应选项的ID"),日期类型使用list(date = "ISO格式时间字符串")
  • 若卡片首次绑定自定义字段无初始item,首次以正确格式发起的PUT请求会自动创建对应item,解决之前遇到的404问题
  • 所有接口鉴权参数、查询参数统一通过httr的query参数列表传递,避免手动拼URL带来的编码错误

内容的提问来源于stack exchange,提问作者dspn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.26 16:36:04