HOWTO: Create a ticket using HappyFox API and C#

Please find the below code to create a ticket using HappyFox API and C#.

Important Note: If the category you are creating the ticket in contains mandatory custom fields, please make sure to add them to the list of values in the postData below.

void CreateTicketUsingHTTPPost()

        {

            string subject = "TEST test ticket from api from .NET";

            string text = "test";

            string email = "john.smith@happyfox.com";

            string t_cf_1 = "2";

            string c_cf_1 = "2";

            string category = "1";

            string priority = "1";

            string user = "1";

            string user_name = "John Smith";

            string postData = "subject=" + subject;

            postData += ("&text=" + text);

            postData += ("&email=" + email);

            postData += ("&category=" + category);

            postData += ("&priority=" + priority);

            postData += ("&t-cf-1=" + t_cf_1);

            postData += ("&c-cf-1=" + c_cf_1);

            postData += ("&name=" + user_name);

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Prepare web request...

            HttpWebRequest myRequest =

              (HttpWebRequest)WebRequest.Create("http://<youripaddress>/api/1.0/xml/tickets/");

            myRequest.Credentials = new NetworkCredential("<YourAuthKey>", "<YourSecret>");

            myRequest.Method = "POST";

            myRequest.ContentType = "application/x-www-form-urlencoded";

            myRequest.ContentLength = byteArray.Length;

            Stream dataStream;

            dataStream = myRequest.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);

            dataStream.Close();

            var response = (HttpWebResponse)myRequest.GetResponse();

        }

  • 21
  • 25-Aug-2015
  • 1828 Views