I'm currently using RT 4.4.3 in a project and I'm trying to create a new ticket with an attachment, using Java code.
I tried to follow the instructions provided by this BestPractical resource hosted on GitHub and specified in this list of pulls.
The code fragment that tries to perform the operation is the following:
PostMethod mPost = new PostMethod(TicketListConstants.SEGNALAZIONI_RTIR_URI + "/ticket");
mPost.setRequestHeader("Content-type", "application/json");
mPost.setRequestHeader("Authorization", TicketListConstants.SEGNALAZIONI_RTIR_TOKEN);
/*String json = ;
NameValuePair[] data = {
new NameValuePair("content", json)
};*/
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
File file = uploadRequest.getFile("fileName");
String filename = uploadRequest.getFileName("fileName");
byte[] filecontent = this.encodeBase64(file);
mPost.setRequestBody("{ \"Queue\": \"Infosharing\", \"Subject\": \""+subject+"\",\"From\":\""+currentUser.getEmailAddress()+"\",\"To\":\"[email protected]\",\"Owner\":\""
+currentUser.getEmailAddress()+"\",\"Requestor\":\""+currentUser.getEmailAddress()+"\",\"Content\":\""+description+"\",\"AttachmentsContents\":[{\"FileName\":\""+filename+"\",\"FileType\":\"application/pdf\",\"FileContent\":\""+filecontent+"\"}]}");
HttpClient cl = new HttpClient();
String result = "";
String newId = "";
try {
cl.executeMethod(mPost);
result = mPost.getResponseBodyAsString();
if (result != null) {
JSONObject json = null;
try {
json = JSONFactoryUtil.createJSONObject(result);
} catch (JSONException e) {
_log.error("Error extracting ticket info: "+e.getMessage());
}
newId = json.getString("id");
}
} catch (UnsupportedEncodingException e){
_log.error("Error in searching tickets: "+e.getMessage());
} catch (IOException io) {
_log.error("Error in searching tickets: "+io.getMessage());
}
So the JSON I'm sending to RT is the following:
{ "Queue": "Infosharing", "Subject": "Tutto in uno","From":"[email protected]","To":"[email protected]","Owner":"[email protected]","Requestor":"[email protected]","Content":"Aggiungo tutto in un solo passaggio","AttachmentsContents":[{"FileName":"prova.txt","FileType":"plain/text","FileContent":""}]}
The problem is that the ticket is correctly created but no attachment is added.
I also tried to perform the same using SOAPUI but no attachment is added to the ticket even if the response is without any error.
Could somebody help me what I'm doing wrong?
EDIT 2019-06-10: since it seems that, as reported here, at least till the end of December 2018:
CREATING ATTACHMENTS Currently RT does not allow creating attachments via their API.
See https://rt-wiki.bestpractical.com/wiki/REST#Ticket_Attachment
but it should be possible, as a temporary workaround, to post attachments to ticket's comments, can anybody help finding a solution to this problem?
Since I cannot test your code, I suggest you to use HttpClient 4, I provide below a sample code snippet. Modify the code as per your requirement and try to check.