ruby - 如何通过 ActiveResource/REST API 将文件上传到 Redmine?

标签 ruby rest file-upload redmine activeresource

我正在尝试将图像批量上传到 Redmine 并将它们链接到特定的 wiki 页面。

文档( Rest_apiUsing the REST API with Ruby )提到了一些方面,但示例在各个方面都失败了。我还尝试从 source 中汲取想法。 - 没有成功。

任何人都可以提供一个简短的示例来演示如何从 Ruby 中上传和链接图像吗?

最佳答案

这有点棘手,因为附件和 wiki API 都相对较新,但我过去也做过类似的事情。这是一个使用 rest-client 的最小工作示例:

require 'rest_client'
require 'json'

key = '5daf2e447336bad7ed3993a6ebde8310ffa263bf'
upload_url = "http://localhost:3000/uploads.json?key=#{key}"
wiki_url = "http://localhost:3000/projects/some_project/wiki/some_wiki.json?key=#{key}"
img = File.new('/some/image.png')

# First we upload the image to get attachment token
response = RestClient.post(upload_url, img, {
  :multipart => true,
  :content_type => 'application/octet-stream'
})
token = JSON.parse(response)['upload']['token']

# Redmine will throw validation errors if you do not
# send a wiki content when attaching the image. So
# we just get the current content and send that
wiki_text = JSON.parse(RestClient.get(wiki_url))['wiki_page']['text']

response = RestClient.put(wiki_url, {
  :attachments => {
    :attachment1 => { # the hash key gets thrown away - name doesn't matter
      :token => token,
      :filename => 'image.png',
      :description => 'Awesome!' # optional
    }
  },
  :wiki_page => {
    :text => wiki_text # original wiki text
  }
})

关于ruby - 如何通过 ActiveResource/REST API 将文件上传到 Redmine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19471089/

相关文章:

ruby - 非破坏性force_encoding?

java - 将上传的文件保存在特定位置

css - 使用文本链接而不是输入上传文件

ruby - 索引超出字符串, ruby

ruby-on-rails - Rails 4 渲染 json 嵌套对象

json - Golang Godog REST API 测试失败

使用 Spring 3.0 Rest Webservice 的 Jquery ajax 跨域策略问题

file-upload - 使用beego上传相同格式的文件

css - 如何在 rspec (Rails 4/rspec3) 上测试文件大小

Project Server 15 的 REST Web 服务