You are not logged in.
Hi,
I'm testing a supervision script who can create a ticket via the API REST (that's ok ), add followup (that's ok too ) and can also update the ticket (change itil categorie, status, prority...) when needed : Here a fail !
i'm writing this with bash / cURL
Thinks I done :
#!/bin/bash
testapi="kwH9y93pWQhYdl6VED******WEagn8ih4YS" # Personal token for the user "testapi"
apptoken="RTxnySuC3PZRHC*****CkS7taaVnXGViu9B"
url="https://myfqdn.com/apirest.php"
# Get an api TOKEN : Works
APISESSION=$(curl -s -X GET -H 'Content-Type: application/json' -H "Authorization: user_token $testapi" -H "App-Token:$apptoken" "$url/initSession" | grep -o -P '(?<=:").*(?=")')
# Post a new ticket : Fine too !
post=$(curl -s -H POST "$url/Ticket" -H 'Content-Type: application/json' -H "app-token:$apptoken" -H "session-token:$APISESSION" -H 'Cookie: glpi_token' -d '{"input":{"name":"titre ticket","content":"contenu du ticket généré par cURL","status":"1","urgency":"1","priority":"4","_users_id_requester":"621","_users_id_observer":"129","_groups_id_assign":"138","itilcategories_id":"50"}}' | grep -o -P '(?<=:").*(?=")')
# Extract ticket ID from $post : Usefull ;)
ticket_id=$(echo $post | grep -o -P '(?<=\()(.*?)(?=\))')
# Add followup for ticket $ticket_id : Great !
curl -s -H POST "$url/Ticket/$ticket_id/TicketFollowup" -H 'Content-Type: application/json' -H "app-token:$apptoken" -H "session-token:$APISESSION" -d '{"input": {"tickets_id":"'$ticket_id'" ,"content":"Ajout d un suivi"}}'
# Modify itil categorie : NOT WORK, WTF ?
curl -s -H PUT "$url/Ticket/$ticket_id/" -H 'Content-Type: application/json' -H "app-token:$apptoken" -H "session-token:$APISESSION" -d '{"input": {"tickets_id":"'$ticket_id'","itilcategories_id":"11"}}'
# End session : Yup...
APIKILL=$(curl -s -X GET -H 'Content-Type: application/json' -H "Session-Token:$APISESSION" -H "App-Token:$apptoken" "$url/killSession" | grep "true" )
The result is a new ticket with it's followup (great) but the modifying categorie cRUL create another new ticket : not good.
I tried many combinaisons of commands and statements (PUT, POST, PATCH, add some "edit" of "modify" at the end of the url, complete values... (seems there's no documentation...)
Thank you for help
Offline
/apirest.php#content-update-items
The documentation does cover this. You need to use PUT or PATCH (PATCH is technically the correct method). Inside the request body, you have a "tickets_id" field which doesn't exist for tickets; it is named "id" for everything. Technically, it should be optional in this case since the ticket's ID is also in the URL, but I've not tested that.
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
Thank you !
Then, read the help, helps right code ligne :
curl -X PATCH "$url/Ticket/$ticket_id/" -H 'Content-Type: application/json' -H "app-token:$apptoken" -H "session-token:$APISESSION" -d '{"input": {"itilcategories_id":"11"}}'
....I just replaced curl -s -H by curl -X
It works fine
Last edited by FranckRC24 (2024-05-16 12:59:08)
Offline