You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2024-09-06 17:53:38

jriccillo
Member
Registered: 2024-07-11
Posts: 3

Issue with Creating ITILCategory via GLPI API: "ERROR_BAD_ARRAY"

Hello,

I'm encountering an issue while trying to create an ITILCategory using the GLPI API. I receive the following error message:

["ERROR_BAD_ARRAY","The input parameter must be an array of objects; see the documentation in your browser at http://your-glpi-url/apirest.php/#ERROR_BAD_ARRAY"]

I've tried sending the request with different formats, but none seem to resolve the issue. Here's a portion of my PHP script:

<?php

$api_url = 'http://url/apirest.php/';
$user_token = 'user_token';
$app_token = 'app_token';


function get_session_token($api_url, $user_token, $app_token) {
    $url = $api_url . 'initSession';
    // cURL setup and execution here
}


function api_post($url, $data, $user_token, $app_token, $session_token) {
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($data),
        CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json",
            "App-Token: $app_token",
            "Session-Token: $session_token",
            "Authorization: user_token $user_token"
        ),
    ));
    $response = curl_exec($curl);
    // Handle response and errors here
}


$post_data = [
    'entities_id' => 13,
    'is_recursive' => 1,
    // Other fields here
];


$url = $api_url . "Entity/13/ITILCategory/";
$response = api_post($url, $post_data, $user_token, $app_token, $session_token);

if ($response && isset($response['id'])) {
    echo "Category created: ID " . $response['id'];
} else {
    echo "Failed to create category. Response: ";
    print_r($response);
}

?>


Additional Information:

    The endpoint used is Entity/13/ITILCategory/.
    I've ensured that all required fields are included and correctly formatted.

Any help or insights on resolving this issue would be greatly appreciated.

Thank you!

Offline

#2 2024-09-07 12:09:24

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,809
Website

Re: Issue with Creating ITILCategory via GLPI API: "ERROR_BAD_ARRAY"

The fields are not formatted correctly. The current API requires all of the data to be in an "input" property like so:
{
    "input": {
        // Your fields here
    }
}


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

Board footer

Powered by FluxBB