![](/img/trans.png)
[英]How to access nested json params in a post request with Ruby on Rails on the backend
[英]Post request shows json key repeated and nested - redmine and ruby
我正在嘗試發布問題以使用其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
需要'rubygems'需要'active_resource'需要'roo'
它應該是custom_field_values
,而不是custom_fields_values
(單個field
)。
我意識到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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.