Consuming the JSON API end point using .NET
osTicket has an HTTP based API end point that can accept JSON payloads (apart from the other end point that accepts XML). This page gives an idea of how to file new tickets using .NET (in a language independent manner) into your osTicket instance. The information given here is based off of https://colab.interlegis.leg.br/browser/osTicket-1.7/setup/doc/api/tickets.md.
The file below represents an example payload that can be sent to the API.
{
"name": "Angry User",
"email": "api@osticket.com",
"phone": "3185558634X123",
"subject": "Testing API",
"ip": "123.211.233.122",
"message": "MESSAGE HERE",
"topicId": "1",
"attachments": [
{"image.png": "data:image/png;base64,R0lGODdhMAA..."},
]
}
You will not need much in order to encode the JSON payload and send it to the osTicket API end point. In fact you will need to use the .NET base class libraries (namely mscorlib.dll and System.dll) together with the JSON.NET library. The latter can be obtained by downloading the Newtonsoft.Json
package from NuGet.org using your favourite NuGet client.
The following steps highlight the method required for encoding and sending a ticket to the JSON API end point.
- Import the System, System.Net, Newtonsoft.Json and System.Collections.Generic namespaces
- Make a
Dictionary<string, object>
instance, and name it ```payload`` - Use the
Add
method on thepayload
dictionary to add key value pairs for all the attributes in the desired JSON. Leave out theattachments
key value pair for now. - Make a new
Dictionary<string,string>
instance namedatts
. - Add key value pairs to
atts
with the key being the file name and the value being a data URL string with the file. (A data URL string would be produced by putting the file mime type and the file's Base64 encoding into the following template:data:{{mime}};base64,{{base64}}
. - Add the entry with
attachments
being the key and aDictionary<string,string>
array havingatts
as its only element as the value to thepayload
dictionary. - Now to send it, make a
WebClient
instance namedclient
. - Add the following headers to the
client.Headers
collection using itsAdd
method:- "X-API-Key", "YOUR API KEY HERE"
- "Expect",
string.Empty
- "User-Agent", "My osTicket Client"
- Next, encode the
payload
using JSON.NET by callingJsonConvert.SerializeObject(payload)
and putting the result in a newstring`` variable named
json```. - Finally send the request using
client.Upload("http://yourdomain.tld/api/http.php/tickets.json", "POST", json)
.