发布请求显示json密钥重复和嵌套 - redmine和ruby

时间:2014-09-23 16:39:51

标签: ruby-on-rails ruby json redmine redmine-api

我正在尝试使用他们的API创建一个问题来解决问题。 他们说我应该使用这种格式。

http://www.redmine.org/projects/redmine/wiki/Rest_api_with_ruby

这是我的代码。

issue = Issue.new(
  :subject => 'Feedback',
  :assigned_to_id => 1,
  :project_id => 1,
  :description => $description,
  :custom_field_values => {"6" => "Thomas"},
)

所有字段都可以正常工作,但最后一个字段除外:custom_field_values。我运行脚本时不会添加自定义字段

当我检查服务器日志时,发布请求会将custom_field_values显示为custom_field_values的键,这会阻止我的导入工作

这是服务器日志

Parameters: {"issue"=>{"assigned_to_id"=>1, "custom_field_values"=>
{"custom_field_values"=>{"6"=>"Thomas"}}, "description"=>"placeholder text", 
"project_id"=>1, "subject"=>" Feedback"}}

当我通过redmine界面创建票证时,正确的JSON看起来像这样。

Parameters: {"utf8"=>"✓", "issue"=>{"is_private"=>"0", "tracker_id"=>"4",  
"subject"=>"test", "description"=>"test", "status_id"=>"1", "priority_id"=>"4", 
"assigned_to_id"=>"", "custom_field_values"=>{"3"=>"Web", "4"=>["Search", ""], 
"5"=>"testeaf", "6"=>"sdfasdfadf", "7"=>"2014-09-30"}}, "commit"=>"Create", 
"project_id"=>"testproject"}

任何人都可以帮我看看为什么我会通过嵌套获得重复的密钥? 如果我组成一个随机密钥,也会发生同样的事情。

我正在使用Bitnami Redmine 2.5 Ruby 2.0 Windows 7

要求' ruby​​gems' 要求' active_resource' 要求' roo'

2 个答案:

答案 0 :(得分:0)

它应该是custom_field_values,而不是custom_fields_values(单数field)。

答案 1 :(得分:0)

我意识到自定义字段的active_resource中存在一些错误。因此,我决定使用NET :: HTTP直接将我的发布请求作为json对象发送,而不是使用它来发送请求。

@user = 'admin'
@pass = 'admin'
@host = 'localhost'
@port = '80'

@payload ={ 
    "issue" => {
    "project_id" => "test_project",
    "subject"=> "test subject",
    "priority_id"=> 4,
    "tracker_id"=> 4,
    "description" => "message",
    "custom_fields"=> [
        {"value"=> name, "id"=> 6},
        {"value"=> date, "id"=> 7},
        {"value"=> email, "id"=> 5}
    ]
  }
  }.to_json

def post
     req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
          req.basic_auth @user, @pass
          req.body = @payload
          response = Net::HTTP.new(@host, @port).start {|http| http.request(req) }
           puts "Response #{response.code} #{response.message}:
          #{response.body}"
        end

thepost = post

此处看到的跟随模式: https://www.socialtext.net/open/very_simple_rest_in_ruby_part_3_post_to_create_a_new_workspace

Ruby send JSON request