-
Notifications
You must be signed in to change notification settings - Fork 181
/
CreateTicketWithAttachment.cs
156 lines (132 loc) · 6.33 KB
/
CreateTicketWithAttachment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.IO;
using System.Net;
using System.Text;
namespace FreshdeskTest
{
class CreateTicketWithAttachment
{
private const string fdDomain = "YOUR_DOMAIN";
private const string _APIKey = "YOUR_API_KEY";
private const string path = "/api/v2/tickets";
private const string _Url = "https://"+ fdDomain + ".freshdesk.com" + path;
private static void writeCRLF(Stream o)
{
byte[] crLf = Encoding.ASCII.GetBytes("\r\n");
o.Write(crLf, 0, crLf.Length);
}
private static void writeBoundaryBytes(Stream o, string b, bool isFinalBoundary)
{
string boundary = isFinalBoundary == true ? "--" + b + "--" : "--" + b + "\r\n";
byte[] d = Encoding.ASCII.GetBytes(boundary);
o.Write(d, 0, d.Length);
}
private static void writeContentDispositionFormDataHeader(Stream o, string name)
{
string data = "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n";
byte[] b = Encoding.ASCII.GetBytes(data);
o.Write(b, 0, b.Length);
}
private static void writeContentDispositionFileHeader(Stream o, string name, string fileName, string contentType)
{
string data = "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + fileName + "\"\r\n";
data += "Content-Type: " + contentType + "\r\n\r\n";
byte[] b = Encoding.ASCII.GetBytes(data);
o.Write(b, 0, b.Length);
}
private static void writeString(Stream o, string data)
{
byte[] b = Encoding.ASCII.GetBytes(data);
o.Write(b, 0, b.Length);
}
static void Main(string[] args)
{
Console.WriteLine("Application starting...");
// Define boundary:
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
// Web Request:
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(_Url);
wr.Headers.Clear();
// Method and headers:
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
// Basic auth:
string login = _APIKey + ":X"; // It could be your username:password also.
string credentials = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(login));
wr.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
// Body:
using (var rs = wr.GetRequestStream())
{
// Email:
writeBoundaryBytes(rs, boundary, false);
writeContentDispositionFormDataHeader(rs, "email");
writeString(rs, "example@example.com");
writeCRLF(rs);
// Subject:
writeBoundaryBytes(rs, boundary, false);
writeContentDispositionFormDataHeader(rs, "subject");
writeString(rs, "Ticket Title");
writeCRLF(rs);
// Description:
writeBoundaryBytes(rs, boundary, false);
writeContentDispositionFormDataHeader(rs, "description");
writeString(rs, "Ticket description.");
writeCRLF(rs);
// Status:
writeBoundaryBytes(rs, boundary, false);
writeContentDispositionFormDataHeader(rs, "status");
writeString(rs, "2");
writeCRLF(rs);
// Priority:
writeBoundaryBytes(rs, boundary, false);
writeContentDispositionFormDataHeader(rs, "priority");
writeString(rs, "2");
writeCRLF(rs);
// Attachment:
writeBoundaryBytes(rs, boundary, false);
writeContentDispositionFileHeader(rs, "attachments[]", "x.txt", "text/plain");
FileStream fs = new FileStream("/path/to/my/file.txt", FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
rs.Write(data, 0, data.Length);
writeCRLF(rs);
// End marker:
writeBoundaryBytes(rs, boundary, true);
rs.Close();
// Response processing:
try
{
Console.WriteLine("Submitting Request");
var response = (HttpWebResponse)wr.GetResponse();
Stream resStream = response.GetResponseStream();
string Response = new StreamReader(resStream, Encoding.ASCII).ReadToEnd();
//return status code
Console.WriteLine("Status Code: {1} {0}", ((HttpWebResponse)response).StatusCode, (int)((HttpWebResponse)response).StatusCode);
//return location header
Console.WriteLine("Location: {0}", response.Headers["Location"]);
//return the response
Console.Out.WriteLine(Response);
}
catch (WebException ex)
{
Console.WriteLine("API Error: Your request is not successful. If you are not able to debug this error properly, mail us at support@freshdesk.com with the follwing X-Request-Id");
Console.WriteLine("X-Request-Id: {0}", ex.Response.Headers["X-Request-Id"]);
Console.WriteLine("Error Status Code : {1} {0}", ((HttpWebResponse)ex.Response).StatusCode, (int)((HttpWebResponse)ex.Response).StatusCode);
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
Console.Write("Error Response: ");
Console.WriteLine(reader.ReadToEnd());
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR");
Console.WriteLine(ex.Message);
}
}
}
}
}