Foreman 1.6.0 APIv2: 415 Unsupported Media Type when creating/updating override value for sc-param

Looks like it… perhaps you can add the following line to
"check_content_type" in
/usr/share/foreman/app/controllers/api/v2/base_controller.rb, then
restart Apache.

logger.warn("Detected content type: #{request.content_type.inspect}")

··· On 07/10/14 13:30, Herwig Bogaert wrote: > Hi > > The following code results in a HTTP error 415 when running in our > Foreman 1.6 environment. > > In our Foreman 1.5.2 environment, this runs just fine. > > Running similar post requests with json data via curl runs fine in both > environments (1.5.2 and 1.6). > > #!/usr/bin/env ruby > > require 'rubygems' > require 'rest-client' > require 'json' > matcher = 'environment=development' > client = RestClient::Resource.new('https://foreman/api', :user => > 'admin', :password => '********', :headers => { :accept => > 'application/json;version=2' }) > newhash = { 'testkey' => "testvalue"}.to_json > response = client["smart_class_parameters/664/override_values"].post( > { :override_value => { :match => matcher,:value => newhash } }, > :content_type => 'application/json') > > The main difference between the two environments is the foreman version, > hence I suspect something is wrong in parsing the request body generated > by ruby rest-client in foreman 1.6. > > Foreman production.log (it does not contain more relevant information > when foreman runs in debug mode).: > > Started POST "/api/smart_class_parameters/664/override_values" for > 10.9.120.2 at 2014-10-07 14:17:57 +0200 > Processing by Api::V2::OverrideValuesController#create as JSON > Parameters: {"override_value"=>"[FILTERED]", "apiv"=>"v2", > "smart_class_parameter_id"=>"664"} > Authorized user admin(Admin User) > Rendered api/v2/errors/unsupported_content_type.json.rabl within > api/v2/layouts/error_layout (1.0ms) > > Is this a bug ?


Dominic Cleal
Red Hat Engineering

Hi Dominic

Thanks for the quick response

In the mean while I was able to get it working on foreman 1.6 by converting
the hash to a json string, that is changing line:

response = client["smart_class_parameters/664/override_values"].post(
{ :override_value => { :match => matcher,:value => newhash } },
:content_type => 'application/json')

to:

response = client["smart_class_parameters/664/override_values"].post(
{ :override_value => { :match => matcher,:value => newhash } }.to_json,
:content_type => 'application/json')

This solves the problem for me, but let me know if you are still interested
in the result of the check-content-type output…

Best regards

Herwig

··· On Tuesday, October 7, 2014 2:37:55 PM UTC+2, Dominic Cleal wrote: > > On 07/10/14 13:30, Herwig Bogaert wrote: > > Hi > > > > The following code results in a HTTP error 415 when running in our > > Foreman 1.6 environment. > > > > In our Foreman 1.5.2 environment, this runs just fine. > > > > Running similar post requests with json data via curl runs fine in both > > environments (1.5.2 and 1.6). > > > > #!/usr/bin/env ruby > > > > require 'rubygems' > > require 'rest-client' > > require 'json' > > matcher = 'environment=development' > > client = RestClient::Resource.new('https://foreman/api', :user => > > 'admin', :password => '********', :headers => { :accept => > > 'application/json;version=2' }) > > newhash = { 'testkey' => "testvalue"}.to_json > > response = client["smart_class_parameters/664/override_values"].post( > > { :override_value => { :match => matcher,:value => newhash } }, > > :content_type => 'application/json') > > > > The main difference between the two environments is the foreman version, > > hence I suspect something is wrong in parsing the request body generated > > by ruby rest-client in foreman 1.6. > > > > Foreman production.log (it does not contain more relevant information > > when foreman runs in debug mode).: > > > > Started POST "/api/smart_class_parameters/664/override_values" for > > 10.9.120.2 at 2014-10-07 14:17:57 +0200 > > Processing by Api::V2::OverrideValuesController#create as JSON > > Parameters: {"override_value"=>"[FILTERED]", "apiv"=>"v2", > > "smart_class_parameter_id"=>"664"} > > Authorized user admin(Admin User) > > Rendered api/v2/errors/unsupported_content_type.json.rabl within > > api/v2/layouts/error_layout (1.0ms) > > > > Is this a bug ? > > Looks like it.. perhaps you can add the following line to > "check_content_type" in > /usr/share/foreman/app/controllers/api/v2/base_controller.rb, then > restart Apache. > > logger.warn("Detected content type: #{request.content_type.inspect}") > > -- > Dominic Cleal > Red Hat Engineering >