Hello all,
I am trying to attach a file to a Jira Task using the API, when I execute the following code, I get the 200 response code, but I see that no attachment was added to the task. Any advise would be appreciated:
public boolean addattachment(String issuenbr, String fileuri, String filedescription){
String method = "POST";
String url = "https://" + serverName + "/rest/api/2/issue/" + issuenbr + "/attachments";
logger.debug("URL IS " + url);
boolean attachmentAdded = false;
try{
String boundary = UUID.randomUUID().toString();
connection = (HttpURLConnection) new URL(url).openConnection();
login(sys_username, sys_password);
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("X-Atlassian-Token", "no-check");
connection.setRequestProperty("file", fileuri);
DataOutputStream request = new DataOutputStream(connection.getOutputStream());
request.writeBytes("--" + boundary + "\r\n");
request.writeBytes("Content-Disposition: form-data; name=\"description\"\r\n\r\n");
request.writeBytes("--" + boundary + "\r\n");
File file = new File(fileuri);
if (filedescription != null)
request.writeBytes(filedescription + "\r\n");
else
request.writeBytes(file.getName() + "\r\n");
request.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileuri + "\"\r\n\r\n");
request.write(FileUtils.readFileToByteArray(file));
request.writeBytes("\r\n--" + boundary + "--\r\n");
request.flush();
int responseCode = connection.getResponseCode();
}catch(Exception e){
InputOutputUtils.logStackTrace(e);
System.out.println("Something wrong");
}
}
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.