1

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?

hoodaticus
  • 3,772
  • 1
  • 18
  • 28
SuperMario45
  • 11
  • 1
  • 4
  • I am still struggling with sending the ticket. Can you please share your code? I this this https://github.com/freshdesk/fresh-samples/blob/v1/jquery_samples/create_ticket.html but it doesn't work. – CodeSlave Feb 08 '17 at 09:32

2 Answers2

3

I also struggled with this.

Have you tried: https://github.com/freshdesk/fresh-samples/blob/v1/jquery_samples/create_with_attachment.html

CodeSlave
  • 457
  • 6
  • 17
0

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);
SuperMario45
  • 11
  • 1
  • 4