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 2023-06-09 09:20:46

kayapo
Member
Registered: 2023-05-30
Posts: 2

[SOLVED] I want to add computer to the ticket by the API

Hi folks,

i have some issues with the API. I want to create a ticket over API and add computer to a ticket.
The ticket creation works perfectly but the adding a computer doesn't.

Here is my javascript code (All the params.* came from Zabbix):

// Prepare the post message
data = {
    'input': {
        'name': params.alert_subject,
        'content': params.alert_message.replace(/(?:\r\n|\r|\n)/g, '<br />') + '\n<a href=' + GLPi.getProblemUrl(params.zabbix_url, params.trigger_id, params.event_id, params.event_source) + '>Link to problem in Zabbix</a>',
        'status': 1,  // Set status "New"
        'urgency': params.event_nseverity
    }
};

After hammering the POST data send to GLPi API.

response = GLPi.request('post', glpi.url + 'apirest.php/Ticket/', data);

This was just for introduction.

Before the code is updating the ticket it searches the computer.

// Search for the computer
criteria = {"criteria": 
    [
        {
            "field": 1,
            "searchtype": "contains",
            "value": params.host_name.split('.')[0].toLowerCase()
        }
    ]
};
computer = GLPi.request('post', glpi.url + 'apirest.php/search/Computer?forcedisplay[0]=2', criteria);

We have in the computer object an id. This id will we use.

// Is the computer exists in GLPi?
if (computer.totalcount === 1) {
    // Update ticket with computer
    Zabbix.log(3, '[ GLPi Webhook ] computer = ' + JSON.stringify(computer));
    var computer_data = {
        "input":{
            "items_id": computer.data[0]["2"],
            "itemtype": "Computer",
            'tickets_id': response.id
        }
    };

    GLPi.request('post', glpi.url + 'apirest.php/Ticket/' + response.id + '/Item_ticket', computer_data);
}

So here is the problem because after sending the request i get an error message.
The log with the response:

1719324:20230608:160635.394 [ GLPi Webhook ] Received response with status code 200 {"totalcount":1,"count":1,"sort":["1"],"order":["ASC"],"data":[{"1":"ITSM-02","2":2}],"content-range":"0-0/1"}
1719324:20230608:160635.395 [ GLPi Webhook ] computer = {"totalcount":1,"count":1,"sort":["1"],"order":["ASC"],"data":[{"1":"ITSM-02","2":2}],"content-range":"0-0/1"}
1719324:20230608:160635.395 [ GLPi Webhook ] Sending request: https://cmdb-01.backoffice/apirest.php/Ticket/179/Item_ticket {"input":{"items_id":2,"itemtype":"Computer","tickets_id":179}}
1719324:20230608:160635.420 [ GLPi Webhook ] Received response with status code 400 ["ERROR_RESOURCE_NOT_FOUND_NOR_COMMONDBTM","resource not found or not an instance of CommonDBTM; view documentation in your browser at https://cmdb-01.backoffice/apirest.php/#ERROR_RESOURCE_NOT_FOUND_NOR_COMMONDBTM"]
1719324:20230608:160635.421 [ GLPi Webhook ] ERROR: Request failed with status code 400 Check debug log for more information.

I think the web request is wrong especially the URL:

https://cmdb-01.backoffice/apirest.php/Ticket/179/Item_ticket
POST data: {"input":{"items_id":2,"itemtype":"Computer","tickets_id":179}}

The items_id is the id of the computer and this is ok, the itsm-02 is the 2nd server in GLPi.

My question is, what is the correct url for adding computer to the ticket?

Last edited by kayapo (2023-06-09 10:36:16)

Offline

#2 2023-06-09 09:40:28

kayapo
Member
Registered: 2023-05-30
Posts: 2

Re: [SOLVED] I want to add computer to the ticket by the API

Ok this was an easy one... big_smile
The URL what i used is really wrong as i thinked
The correct is apirest.php/Ticket/<tickets_id>/Item_Ticket

Offline

Board footer

Powered by FluxBB