Created
August 26, 2013 12:30
-
-
Save dhlavaty/6340937 to your computer and use it in GitHub Desktop.
Create new Zendesk.com ticket - simplest possible C# code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
namespace DHlavaty | |
{ | |
public class Zendesk | |
{ | |
/* | |
* The biggest headache was to figure out, that Zendesk servers could not parse ByteOrderMark in HTTP request. | |
* So you cannot use "System.Text.Encoding.UTF8" but you must use "new System.Text.UTF8Encoding(false)" instead | |
* in your requests. | |
* | |
* */ | |
public int CreateZendeskTicket(string subject, string message, string senderName, string senderEmail) | |
{ | |
if (String.IsNullOrWhiteSpace(subject) || String.IsNullOrWhiteSpace(message) || String.IsNullOrWhiteSpace(senderName) || String.IsNullOrWhiteSpace(senderEmail)) | |
{ | |
return -1; | |
} | |
string jsonToSend = "{\"ticket\":{\"subject\":\"" + subject + "\",\"comment\":{\"body\":\"" + message + "\"},\"requester\":{\"name\":\"" + senderName + "\",\"email\":\"" + senderEmail + "\"}}}"; | |
string responseData = null; | |
string responseHeaders = null; | |
int responseStatusCode = 0; | |
const string zendeskDomain = "yourdomain.zendesk.com"; // <-- your Zendesk domain | |
const string zendeskLoginEmail = "your@email.here.com"; // <-- add your login here | |
const string zendeskApiKey = "abcdvMPtSPkfh37f1zNWXseNWD3g9Ur7rsjpabcd"; // <-- add your ApiKey here | |
try | |
{ | |
var wc = new WebClientEx(); | |
wc.Proxy = null; | |
wc.Headers.Add(System.Net.HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(zendeskLoginEmail + "/token:" + zendeskApiKey))); | |
wc.Headers.Add(System.Net.HttpRequestHeader.ContentType, "application/json"); | |
var wr = wc.GetWebRequestX(new Uri("https://" + zendeskDomain + "/api/v2/tickets.json")); | |
wr.Method = System.Net.WebRequestMethods.Http.Post; | |
using (StreamWriter writer = new StreamWriter(wr.GetRequestStream(), new System.Text.UTF8Encoding(false))) // <--- DO NOT USE System.Text.Encoding.UTF8 IN HERE, because it emits ByteOrderMark | |
{ | |
writer.WriteLine(jsonToSend); | |
} | |
using (System.Net.WebResponse response = wr.GetResponse()) | |
{ | |
WebClientEx.ProcessResponse(response, out responseData, out responseHeaders, out responseStatusCode); | |
} | |
} | |
catch (System.Net.WebException ex) | |
{ | |
using (ex.Response) | |
{ | |
WebClientEx.ProcessResponse(ex.Response, out responseData, out responseHeaders, out responseStatusCode); | |
} | |
responseData = "<!-- " + ex.Message + " -->\r\n\r\n" + responseData; | |
} | |
return responseStatusCode; | |
} | |
public class WebClientEx : WebClient | |
{ | |
/// <summary> | |
/// See <see cref="System.Net.WebRequest.GetWebRequest"/> | |
/// </summary> | |
/// <param name="address">See <see cref="System.Net.WebRequest.GetWebRequest"/></param> | |
/// <returns>See <see cref="System.Net.WebRequest.GetWebRequest"/></returns> | |
public virtual System.Net.WebRequest GetWebRequestX(Uri address) | |
{ | |
return this.GetWebRequest(address); | |
} | |
public static void ProcessResponse(System.Net.WebResponse response, out string serverResponseData, out string responseHeaders, out int responseStatusCode) | |
{ | |
serverResponseData = responseHeaders = String.Empty; | |
responseStatusCode = 0; | |
using (var responseStream = response.GetResponseStream()) | |
{ | |
using (System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, Encoding.UTF8)) | |
{ | |
serverResponseData = sr.ReadToEnd(); | |
} | |
} | |
responseHeaders = response.Headers.ToString(); | |
var httpWebResponse = response as System.Net.HttpWebResponse; | |
if (httpWebResponse != null) | |
{ | |
responseStatusCode = (int)httpWebResponse.StatusCode; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment