我正在为 Redmine 平台开发一个插件,我想使用链接而不是表单将文件附加到文档(基本上是上传文件),为此我在方法。
我按照说明操作 here ,我根据要求将内容类型设置为 application/octet-stream
然后我将文件内容放入请求正文中。
我在这个网站上阅读了很多帖子,我知道这个问题经常被问到,但我无法正确地执行我的请求,我仍然遇到错误。这是我的代码:
uri = URI.parse("http://<my_server_IP_address>:3000/uploads.js")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.path, initheader = {'X-CSRF-Token' => form_authenticity_token, 'Content-Type' => 'application/octet-stream'})
file = File.new("/home/testFile.txt", 'rb')
request.body = file.read
@response = http.request(request)
如您所见,我使用 form_authenticity_token
方法在 header 中设置了 CSRF token ,但我仍然收到 422 错误。
Filter chain halted as :verify_authenticity_token rendered or redirected
Completed 422 Unprocessable Entity in 4.7ms (ActiveRecord: 0.0ms)
我还尝试将 skip_before_filter :verify_authenticity_token
放在我的 Controller 的开头,尽管不推荐这样做,但它也不起作用。
你知道这里出了什么问题吗?
注意:我正在使用 Rails 3.2.16、Ruby 1.9.3-p392 和 Redmine 2.4.2
最佳答案
您是要 POST 到“uploads.js”而不是“uploads.json”吗?
uri = URI.parse("http://<my_server_IP_address>:3000/uploads.js")
文档表明您 POST 到 uploads.json 或 uploads.xml 似乎是基于您希望接收响应的内容格式。
(本来可以对你的问题发表评论,但我还没有那个业力)
关于ruby-on-rails - Rails/Redmine - 无法验证真实性 token 导致 422 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22685377/