I want to add a ticket with an attachment to Freshdesk via the API. I know how to add a ticket without an attachment, and it's working fine. However, I don't know how to add a ticket with an attachment. I want to do this with JSON. I tried something like this:
string json = $"{{\"helpdesk_ticket\": {{\"subject\":\"{subject}\",\"description_html\":\"{fullDescription}\",\"name\":\"{user}\",\"attachments\":{{\"\":[{{\"resource\":\"{bytes}\"}}]}}}}}}";
In the bytes field I have my file bytes array. But it's not working. Can someone help me to pass a file in JSON to the Freshdesk API?
I also struggled with this.
Have you tried: https://github.com/freshdesk/fresh-samples/blob/v1/jquery_samples/create_with_attachment.html
I resolved this problem with RestSharp. This is simple tool to REST API. When i'm sending tickets with attachments i use this code:
var client = new RestClient(_freshdeskUrl);
client.Authenticator = new HttpBasicAuthenticator(_apiKey, "X");
var request = new RestRequest("", Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddParameter("email", "example@example.com");
request.AddParameter("subject", "Subject");
request.AddParameter("description", "Description");
request.AddParameter("name", "Name");
request.AddParameter("status", "2");
request.AddParameter("priority", "1");
request.AddFile("attachments[]", bytes, "Logs.txt", "text/plain");
var response = client.Execute(request);
And when i'm sending ticket without attachment I use this code:
RestClient client = new RestClient(_freshdeskUrl);
client.Authenticator = new HttpBasicAuthenticator(_apiKey, "X");
RestRequest request = new RestRequest("", Method.POST);
request.AddHeader("Accept", "application/json");
request.AddJsonBody(new
{
email = "example@example.com",
subject = "Subject",
description = "Description",
name = "Name",
status = 2,
priority = 1
});
var response = client.Execute(request);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.