You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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");
}
}