1

I am trying to create a post an issue to redmine using their API. They say that I should use this format.

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

Here is my code.

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

All the fields work fine, except for the last one named :custom_field_values. It won’t add the custom field when I run the script

When I check the server log the post request shows custom_field_values as a key of custom_field_values which prevents my import from working

This is the server log

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

When I created a ticket though the redmine interface the correct JSON looks like this.

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"}

Can anyone help me to see why I am getting that duplicated key with nesting? Same thing happens if I make up a random key.

I am using Bitnami Redmine 2.5 Ruby 2.0 Windows 7

require 'rubygems' require 'active_resource' require 'roo'

AndraeRay
  • 2,348
  • 2
  • 12
  • 9

2 Answers2

0

It should be custom_field_values, not custom_fields_values (singular field).

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • Thanks for pointing it out. I made the typo somewhere along testing. I still see the key getting duplicated after changing. – AndraeRay Sep 23 '14 at 17:02
0

I realized there is some bug in the active_resource for custom fields. So instead of using it to send the request I decided to use NET::HTTP to send my post request as a json object directly.

@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

Followed pattern seen here: https://www.socialtext.net/open/very_simple_rest_in_ruby_part_3_post_to_create_a_new_workspace

Ruby send JSON request

Community
  • 1
  • 1
AndraeRay
  • 2,348
  • 2
  • 12
  • 9