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 2017-07-19 20:41:03

hypemonkey
Member
Registered: 2017-06-05
Posts: 8

REST API - Create a Ticket - VB.NET

Hi,
for the sake of helping others, I will post my solution here (for documentation too).

First thing is to setup the API Client in GLPI (you will need a Super-Admin account):

Go to -> Home -> Setup -> General -> API

Make sure "Enable Rest API" is set to "Yes" / "Enable login with credentials"  is set to "Yes"

Click on "Add API client".

Give it a name / Make sure to set "Active" to "Yes" / Always useful to have "Log connections" set to "Historical"

Click on "Add"

Before we even start to code, we will need the App-Token.
To get it, go back to -> Home -> Setup -> General -> API.
Select the API Client you just created. Application token (app_token) is what you are looking for <- Copy that somewhere, we will need it later.

THE CODE:

Create a Ticket using the REST API of the version (9+):
Please note: the code is not perfect, but when I started, I wish I had something like this to help me out smile

First, we want to get a Session-Token. To do that, we use the initSession function. It will return the token.
To store my Session-Token, I use a custom class.

Public Class GLPISession
    Public session_token As String
End Class

Then we call initSession - You will need you App-Token / A GLPI's username + password:

Dim currentsessiontoken As GLPISession

Dim sUrl As String = "http://glpi/apirest.php/initSession/?app_token=XXX&login=XXX&password=XXX"

Dim wRequest As HttpWebRequest = WebRequest.CreateHttp(sUrl)
wRequest.ContentType = "application/json"
wRequest.Method = "GET"
wRequest.Accept = "application/json"
Using wResponse As HttpWebResponse = wRequest.GetResponse()
    Dim sResponse As String = ""

    Using srRead As New StreamReader(wResponse.GetResponseStream())
        sResponse = srRead.ReadToEnd()
        currentsessiontoken = JsonConvert.DeserializeObject(Of GLPISession)(sResponse)
    End Using
End Using

Ok, now we have our Session-Token.

The next step is to get ready to call the Ticket createItems.
We will need some data + we will pass it in JSON. Here is the JSON format:

{
    "input":
    {
         "name":"Title here",
         "content":" Ticket Description here",
         "status":"1",
         "urgency":"2",
         "_disablenotif":true
    }
}

Please note, I did not find every fields we can pass through the JSON, but what I have for the moment is a good start.

To replicate the json in VB.NET, I use these two class:

Public Class JsonGLPITicket
    Public input As GLPITicket
End Class

Public Class GLPITicket
    Public name As String
    Public content As String
    Public status As String
    Public urgency As String
    Public _disablenotif As Boolean
End Class

Put the data in our object. Then use JsonConvert.SerializeObject() to convert our object to JSON:

Dim jsonglpiticket As JsonGLPITicket = New JsonGLPITicket()
Dim newticket As GLPITicket = New GLPITicket()
newticket.name = "Title here"
newticket.content = "Description here"
newticket.status = 1
newticket.urgency = 2
newticket._disablenotif = True
jsonglpiticket.input = newticket

Dim jsonglpiticketConverted as string = JsonConvert.SerializeObject(jsonglpiticket) 

With our Session-Token & the data in JSON, we are almost ready to create the Ticket.
We miss a response class. When we add a Ticket, GLPI return JSON with a message and an ID (if the call is successful). It might be interesting to store these information in a class using the JsonConvert.DeserializeObject().

Public Class GLPICreatedTicketResponse
    Public id As String
    Public message As String
End Class

Time to call the API:

Dim sUrl2 As String = "http://glpi/apirest.php/Ticket/"
Dim wRequest2 As HttpWebRequest = WebRequest.CreateHttp(sUrl2)
wRequest2.ContentType = "application/json"
wRequest2.Method = "POST"
wRequest2.Accept = "application/json"
wRequest2.Headers.Add("App-Token", "XXX")
wRequest2.Headers.Add("Session-Token", currentsessiontoken.session_token)
Dim ticketstring As String = jsonglpiticketConverted
Dim postdatabytes As Byte()
postdatabytes = Encoding.UTF8.GetBytes(ticketstring)
wRequest2.ContentLength = postdatabytes.Length
Using stream = wRequest2.GetRequestStream()
    stream.Write(postdatabytes, 0, postdatabytes.Length)
End Using
Using result As HttpWebResponse = wRequest2.GetResponse()
    Dim sResponse As String = ""

    Using srRead As New StreamReader(result.GetResponseStream())
        sResponse = srRead.ReadToEnd()
        glpicreatedticketresponse = JsonConvert.DeserializeObject(Of GLPICreatedTicketResponse)(sResponse)
     End Using
End Using

If everything went fine, you just created a Ticket in GLPI using the REST API.

Once we are done, we kill the session:

Dim sUrl3 As String = "http://glpi/apirest.php/killSession/"
Dim wRequest3 As HttpWebRequest = WebRequest.CreateHttp(sUrl3)
wRequest3.ContentType = "application/json"
wRequest3.Method = "GET"
wRequest3.Accept = "application/json"
wRequest3.Headers.Add("App-Token", "XXX")
wRequest3.Headers.Add("Session-Token", currentsessiontoken.session_token.ToString)
wRequest3.KeepAlive = False
wRequest3.AllowAutoRedirect = False
Using wResponse As HttpWebResponse = wRequest3.GetResponse()
End Using

Thats it.

I plan to add more stuff later on (How to add assigned / watcher / requester).

Big up at the peeps who helped me figure it out!

Ma boys from GitHub / Forum:
@btry
@ladenree76000 / @LaDenrée
@yllen

Offline

#2 2017-07-24 09:11:12

xarope
Member
Registered: 2017-07-24
Posts: 1

Re: REST API - Create a Ticket - VB.NET

Thanks, I've been looking for a way to auto-create assets from another service, so this gives some good pointers on the usage of REST and JSON input.

Where did you manage to find the information on the JSON fields?  As mentioned, I'd want to do something similar for assets, including date of purchase, date of delivery etc.

Offline

Board footer

Powered by FluxBB