diff --git a/src/apps/vmware/vsphere8/custom/api.pm b/src/apps/vmware/vsphere8/custom/api.pm index d9c8af6677..60e346f915 100644 --- a/src/apps/vmware/vsphere8/custom/api.pm +++ b/src/apps/vmware/vsphere8/custom/api.pm @@ -24,7 +24,6 @@ use strict; use warnings; use centreon::plugins::http; use centreon::plugins::statefile; -use JSON::XS; use MIME::Base64; use Digest::MD5 qw(md5_hex); @@ -80,7 +79,6 @@ sub check_options { $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; $self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : ''; $self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : ''; - #$self->{http_backend} = (defined($self->{option_results}->{http_backend})) ? $self->{option_results}->{http_backend} : 'curl'; if ($self->{hostname} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); @@ -120,7 +118,7 @@ sub settings { return 1 if (defined($self->{settings_done})); $self->build_options_for_httplib(); - $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->set_options(%{$self->{option_results}}); $self->{settings_done} = 1; @@ -144,17 +142,19 @@ sub get_token { || $options{force_authentication} ) { my $auth_string = MIME::Base64::encode_base64($self->{username} . ':' . $self->{password}); + chomp $auth_string; $self->settings(); my $content = $self->{http}->request( method => 'POST', url_path => '/api/session', + query_form_post => '', unknown_status => $self->{unknown_http_status}, warning_status => $self->{warning_http_status}, critical_status => $self->{critical_http_status}, header => [ - 'Content-Type: application/json', - 'Authorization: Basic ' . $auth_string + 'Authorization: Basic ' . $auth_string, + 'Content-Type: application/x-www-form-urlencoded' ] ); @@ -181,7 +181,6 @@ sub try_request_api { header => [ 'vmware-api-session-id: ' . $token ], unknown_status => '', insecure => (defined($self->{option_results}->{insecure}) ? 1 : 0) - ); if (!defined($content) || $content eq '') { @@ -191,14 +190,7 @@ sub try_request_api { $self->{output}->option_exit(); } - my $decoded; - eval { - $decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($content); - }; - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); - $self->{output}->option_exit(); - } + my $decoded = centreon::plugins::misc::json_decode($content); return $decoded; } @@ -355,6 +347,8 @@ Calls try_request_api and recalls it forcing authentication if the first call fa =over 8 +=item * C - The HTTP method to use (examples: GET, POST). + =item * C - The API endpoint to request. =item * C - Additional GET parameters for the request. diff --git a/src/centreon/plugins/misc.pm b/src/centreon/plugins/misc.pm index 627a953147..b44cd710c1 100644 --- a/src/centreon/plugins/misc.pm +++ b/src/centreon/plugins/misc.pm @@ -770,8 +770,7 @@ sub json_encode { $encoded = encode_json($object); }; if ($@) { - use Data::Dumper; - print STDERR 'Cannot encode object to JSON' . Dumper($object); + print STDERR 'Cannot encode object to JSON. Error message: ' . $@; return undef; } diff --git a/tests/apps/vmware/vsphere8/esx/discovery.robot b/tests/apps/vmware/vsphere8/esx/discovery.robot index 0fc9d8337f..002003c036 100644 --- a/tests/apps/vmware/vsphere8/esx/discovery.robot +++ b/tests/apps/vmware/vsphere8/esx/discovery.robot @@ -6,6 +6,7 @@ Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}resources/import.resour Suite Setup Start Mockoon ${MOCKOON_JSON} Suite Teardown Stop Mockoon Test Timeout 120s +Test Setup Ctn Cleanup Cache *** Variables *** diff --git a/tests/apps/vmware/vsphere8/esx/host-status.robot b/tests/apps/vmware/vsphere8/esx/host-status.robot index 88517e0093..f0ac4a625d 100644 --- a/tests/apps/vmware/vsphere8/esx/host-status.robot +++ b/tests/apps/vmware/vsphere8/esx/host-status.robot @@ -6,7 +6,7 @@ Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}resources/import.resour Suite Setup Start Mockoon ${MOCKOON_JSON} Suite Teardown Stop Mockoon Test Timeout 120s - +Test Setup Ctn Cleanup Cache *** Variables *** ${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json