Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add an attachment to jira using REST API in java?

Seema Itagi January 1, 2019 edited

public boolean addAttachmentToIssue(String issueKey, String path){


String auth = new String(org.apache.commons.codec.binary.Base64.encodeBase64((sn@gmail.com+":"+sitgi$123).getBytes()));


HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(baseURL+"issue/"+issueKey+"/attachments");
httppost.setHeader("X-Atlassian-Token", "nocheck");
httppost.setHeader("Authorization", "Basic "+auth);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

File fileToUpload = new File(path);
FileBody fileBody = new FileBody(fileToUpload, "application/octet-stream");
entity.addPart("file", fileBody);

httppost.setEntity(entity);
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
return false;
} catch (IOException e) {
return false;
}
HttpEntity result = response.getEntity();

if(response.getStatusLine().getStatusCode() == 200)
return true;
else
return false;

}

3 answers

1 accepted

0 votes
Answer accepted
Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 1, 2019 edited

Hi,

Here is a REST API using JAVA that works for me:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https:<your_jira_url>/rest/api/latest/issue/"+issueKey+"/attachments");
httppost.setHeader("X-Atlassian-Token", "no-check");
httppost.setHeader("Authorization", "Basic " + credentials);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(file, "application/octet-stream");
entity.addPart("file", fileBody);

httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);

Let me know if you succeedd or not.

If you got any errors, share the errors, but this should work, unless you set 1 or more parameters wrong.

Seema Itagi January 3, 2019

What is fileBody and IssueKey?

Sorry I am new to automation Plz bare me for silly questions.

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 3, 2019

Filebody is how you need to pass your file to the request

so, for example, you have file: "C:\users\Itagi\desktop\file.txt"

so the code will be:

File file = new File ("C:\users\Itagi\desktop\file.txt");
FileBody fileBody = new FileBody(file, "application/octet-stream");
entity.addPart("file", fileBody);

 And IssueKey is the Jira issue you want to upload to the file to ("AJS-123")

0 votes
Sachintha
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 18, 2019

Code ran successfully but attachment not uploaded?

 

String auth = new String(org.apache.commons.codec.binary.Base64.encodeBase64(("USERNAME"+":"+"PASSWORD").getBytes()));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(""URL" + "AUT-23"+"/attachments");
httppost.setHeader("X-Atlassian-Token", "TOKEN");
httppost.setHeader("Authorization", "Basic " + auth);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File ("C:\\Users\\PTL_PC\\IdeaProjects\\API2\\uploads\\Bank.PNG");
FileBody fileBody = new FileBody(file, "application/octet-stream");
entity.addPart("file", fileBody);

httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
0 votes
Abhilash
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 25, 2019

What is issue key ??

Deepashree Mohan
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 1, 2019

Issue Key is something like "QA-123" , "DEV-111" etc

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events