Nilesh
(Nilesh Karotra)
1
I am able to create a ticket and add an attachment.
But when I try to update a ticket with another attachment,
I am not able to do it,
For an update, I am using api/v2/tickets/[ticketid]
Is there any other API to add attachments to existing tickets?
Please help !!
I am trying to do the same thing, updating a ticket to add an attachment. And I am also not successful in doing this, sadly,
But you were able to create a ticket with attachments, how did you do that? I am very interested, thanks!
I have it working:
using System;
using System.IO;
using System.Net;
using System.Text;
namespace testFreshdeskAttachment
{
class UpdateTicketWithAttachment
{
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 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);
}
public static void UpdateIt(long ticketId)
{
Console.WriteLine("Application starting...");
// Define boundary:
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
// Web Request:
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create($"{_Url}/{ticketId}");
wr.Headers.Clear();
// Method and headers:
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "PUT";
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())
{
// Attachment:
writeBoundaryBytes(rs, boundary, false);
writeContentDispositionFileHeader(rs, "attachments[]", "x.txt", "text/plain");
FileStream fs = new FileStream(@"C:\Users\pcoolen\source\repos\testFreshdeskAttachment\testFreshdeskAttachment\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);
}
}
}
}
}