我目前在一个项目中使用RT 4.4.3,并且正在尝试使用Java代码创建带有附件的新票证。
我尝试遵循托管在GitHub上的this BestPractical resource提供的说明,并在此列表列表中指定了此说明。
尝试执行该操作的代码片段如下:
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\":\"test@liferay.com\",\"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());
}
所以我要发送给RT的JSON是以下内容:
{ "Queue": "Infosharing", "Subject": "Tutto in uno","From":"test@liferay.com","To":"test@liferay.com","Owner":"test@liferay.com","Requestor":"test@liferay.com","Content":"Aggiungo tutto in un solo passaggio","AttachmentsContents":[{"FileName":"prova.txt","FileType":"plain/text","FileContent":""}]}
问题在于票证已正确创建,但未添加附件。
我还尝试使用SOAPUI执行相同的操作,但是即使响应没有任何错误,也没有将附件添加到故障单中。
有人可以帮我做错什么吗?
编辑2019-06-10:,因为据here报道,至少到2018年12月底为止:
创建附件当前,RT不允许创建附件 通过他们的API。
请参见https://rt-wiki.bestpractical.com/wiki/REST#Ticket_Attachment
但是,作为一种临时解决方法,应该可以在票证注释中张贴附件,有人可以帮助找到解决该问题的方法吗?
答案 0 :(得分:0)
由于我无法测试您的代码,因此建议您使用HttpClient 4,我在下面提供了一个示例代码片段。根据您的要求修改代码,然后尝试检查。
HttpPost post = new HttpPost("http://rtserver.com");
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
StringBody stringBody1 = new StringBody("Message 1", ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
builder.addPart("text1", stringBody1);
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);