我想通过API添加一张附有Freshdesk附件的票证。我知道如何在没有附件的情况下添加票证,而且工作正常。但是,我不知道如何添加带附件的票证。我想用JSON做到这一点。我试过这样的事情:
string json = $"{{\"helpdesk_ticket\": {{\"subject\":\"{subject}\",\"description_html\":\"{fullDescription}\",\"name\":\"{user}\",\"attachments\":{{\"\":[{{\"resource\":\"{bytes}\"}}]}}}}}}";
在bytes字段中,我有我的文件字节数组。但它不起作用。 有人可以帮我把JSON中的文件传递给Freshdesk API吗?
答案 0 :(得分:3)
我也很挣扎。
你试过了吗? https://github.com/freshdesk/fresh-samples/blob/v1/jquery_samples/create_with_attachment.html
答案 1 :(得分:0)
我用RestSharp解决了这个问题。这是REST API的简单工具。 当我发送带附件的门票时,我使用此代码:
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);
当我发送没有附件的票时,我使用这段代码:
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);