Learn in this article how to call WebService REST APIs of OTRS / Znuny / OTOBO using cURL.
In the previous articles Access Znuny & OTRS API via REST, Znuny & OTRS REST API Routes and Authenticate to Znuny / OTRS REST API we guide you thru the configuration steps of REST API in the role of the service provider. If you followed the steps your system is accessible via new REST API and awaiting now your cURL accesses of this article
REST API Call
As we import the default configuration of the REST API, the web service introduces new routes which are created in the branch:
http://{{znunyHostName}}/otrs/nph-genericinterface.pl/Webservice/Ticket%20REST%20API
The available routes are described in the article (LINK)REST API Routes(/LINK). As we already know the authentication of the HTTP requests is done via one of two methods:
- To authenticate each request with UserLogin and Password
OR - To authenticate with UserLogin and Password to create Session and authenticate all next calls with SessionID.
Authentication information is sent with request data as options. The data format is given by used HTTP method (i.e. for GET requests it has to be mentioned in Query string; for POST&PATCH in JSON Data). Alternatively starting from the version of Znyny 6.1.2 the authentication information can be posted also via X-OTRS-Header-(UserLogin|CustomerUserLogin|SessionID|Password).
Example: Create a Ticket using user/password authenticated request
Below you can see an example of user/password authenticated request creating a Ticket:
Request:
POST http://{{FQDN}}/otrs/nph-genericinterface.pl/Webservice/Ticket%20REST%20API/Ticket?
Data in the body:
{
"data" : {
"UserLogin":"{{user}}",
"Password":"{{pass}}",
"Ticket":{
"Title":"Ticket created via REST API - minimal content",
"Queue":"Raw",
"StateID": 1 ,
"PriorityID": 3,
"CustomerUser":"{{CustomerUser}}"
},
"Article":{
"CommunicationChannel":"Email",
"Subject":"Test Article created with new Ticket via REST API type Email",
"Body":"email body.",
"ContentType":"",
"Charset":"utf-8",
"MimeType":"text/plain"
}
}
}
Variables:
Variable | Description |
{{user}} | User name used to authenticate to OTRS authorized to create Ticket. |
{{pass}} | User’s password |
{{FQDN}} | The fully qualified domain name of the server with OTRS |
{{CustomerUser}} | Login name of the Customer for which the Ticket has been open |
When converted into cURL the example of UserLogin&Password authenticated creation of a ticket looks like this:
curl --location --request POST 'http://localhost/otrs/nph-genericinterface.pl/Webservice/Ticket%20REST%20API/Ticket' --header 'Content-Type: application/json' --data-raw '{ "UserLogin":"userXYZ", "Password":"plain-text-password", "Ticket":{ "Title":"Ticket created via REST API - minimal content", "Queue":"Raw", "StateID": 1 , "PriorityID": 3, "CustomerUser":"CustomerUser" }, "Article":{ "CommunicationChannel":"Email", "Subject":"Test Article created with new Ticket via REST API type Email", "Body":"email body.", "ContentType":"", "Charset":"utf-8", "MimeType":"text/plain" } }'
Example: Create Ticket using SessionID authentication
The second method to create a ticket is to Create Session and then create a ticket being authenticated with the SessionID.
- Create Session and get SessionID
- Create Ticket using SessionID authentication
Create Session and get SessionID
Request:
curl --location --request POST 'http://localhost/otrs/nph-genericinterface.pl/Webservice/Ticket%20REST%20API/Session'
--header 'Content-Type: application/json'
--data-raw '{
"UserLogin":"userXYZ",
"Password":"plain-text-password"
}'
Response:
{
"SessionID": "Yx9TilyRyGveitmMKrdBy2Q8oqKqA4U2"
}
Create Ticket
CURL example of the SessionID authenticated creation of a ticket:
curl --location --request POST 'http://localhost/otrs/nph-genericinterface.pl/Webservice/Ticket%20REST%20API/Ticket'
--header 'Content-Type: application/json'
--data-raw '{
"SessionID":"Yx9TilyRyGveitmMKrdBy2Q8oqKqA4U2",
"Ticket":{
"Title":"Ticket created via REST API - minimal content",
"Queue":"Raw",
"StateID": 1 ,
"PriorityID": 3,
"CustomerUser":"CustomerL"
},
"Article":{
"CommunicationChannel":"Email",
"Subject":"Test Article created with new Ticket via REST API type Email",
"Body":"email body.",
"ContentType":"",
"Charset":"utf-8",
"MimeType":"text/plain"
}
}'
Related articles
To learn more about OTRS & Znuny REST API routes and request options please see (LINK)REST API Routes(/LINK).
To learn how to setup the REST API in OTRS & Znuny installation please see Access Znuny & OTRS API via REST.