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;
}
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.
What is fileBody and IssueKey?
Sorry I am new to automation Plz bare me for silly questions.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is issue key ??
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Issue Key is something like "QA-123" , "DEV-111" etc
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!
Help us plant more trees
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.