You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rails项目中向Trello API发起PUT请求?现有代码报301错误

解决Trello API PUT请求301重定向错误

301错误是因为Trello API强制要求使用HTTPS协议,你的代码当前使用普通HTTP请求,服务器会将其永久重定向到HTTPS地址。以下是修改后的可行代码:

require 'net/http'

# 初始化HTTPS连接
http = Net::HTTP.new('api.trello.com', 443)
http.use_ssl = true

# 发起PUT请求
response = http.send_request(
  'PUT',
  '/1/cards/xxxxxxcardidherexxxxxx?key=xxxxxxkeyherexxxxxx&token=xxxxxxtokenherexxxxxx&idList=xxxxxxtargettedlistidherexxxxxx'
)

关键修改说明:

  • 指定HTTPS默认端口443,并开启use_ssl = true,确保请求通过HTTPS协议发送
  • 原有请求路径和参数无需改动,仅调整连接协议配置

也可以使用更规范的URL编码写法,避免参数拼接可能出现的问题:

require 'net/http'

uri = URI('https://api.trello.com/1/cards/xxxxxxcardidherexxxxxx')
uri.query = URI.encode_www_form(
  key: 'xxxxxxkeyherexxxxxx',
  token: 'xxxxxxtokenherexxxxxx',
  idList: 'xxxxxxtargettedlistidherexxxxxx'
)

Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
  response = http.put(uri)
  # 这里可以添加响应处理逻辑,比如判断响应状态码
end

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 15:47:34