I'm working on a Java project to add attachments to an existing jira issue.
I'm using below Jira resource, which works fine from postman.
POST /rest/api/2/issue/{issueIdOrKey}/attachments
But when I try to achieve the same thing using java it's not working as expected. Below is the piece of code I'm using in Java. Actually, this rest call is going fine with no errors, but the attachment is NOT appearing in Jira UI.
Below java response is NOT matching with what I'm getting in postman.
Java code :
public void addAttachment(String filename , String issueId) throws Exception
{
RestTemplate restTemplate = new RestTemplate();
headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setBasicAuth("username", "password");
headers.set("X-Atlassian-Token", "no-check");
File file = new File(filename);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("files", file);
HttpEntity<MultiValueMap<String, Object>> requestEntity
= new HttpEntity<>(body, headers);
String addAttachUrl = "https://jirainstance.com/rest/api/2/issue/"+issueId+"/attachments";
ResponseEntity<String> output = restTemplate.postForEntity(addAttachUrl, requestEntity, String.class);
LOG.info(output.getBody());
}
RESPONSE :
<200,[],[Date:"Thu, 22 Oct 2020 20:09:48 GMT", Server:"Apache", X-AREQUESTID:"969x3612218x1", X-XSS-Protection:"1; mode=block", X-Content-Type-Options:"nosniff", X-Frame-Options:"SAMEORIGIN", Content-Security-Policy:"frame-ancestors 'self'", X-ASEN:"SEN-2785822", X-Seraph-LoginReason:"OK", X-ASESSIONID:"4mlpm2", X-AUSERNAME:"206410801", Cache-Control:"no-cache, no-store, no-transform", Content-Type:"application/json;charset=UTF-8", Set-Cookie:"JSESSIONID=3AF3DF62B49EC36313A4FCFE3CE69F78; Path=/; HttpOnly", "atlassian.xsrf.token=BXAJ-8ECA-NR3P-YUW5_9cd43f77837537f6850277c8a77cbc424bfda405_lin; Path=/", Vary:"Accept-Encoding", Keep-Alive:"timeout=15, max=100", Connection:"Keep-Alive", Transfer-Encoding:"chunked"]>
Thanks,