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,
I found the solution. Looks like I"m not passing the file content along with the request here.
Thats the reason I was getting 200 response but no attachment in Jira.
Gopi, if you post the code that did eventually work here, others might derive value from that if they run into a similar problem!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am facing similar issue. Can you please share the code which worked for you. That will help me resolve the issue.
I am facing issue when importing the test results from testng-results.xml. Though other execute details gets updated on the XRay test execution but the attachment doesn't. i have tried quite a few solution but didn't work.
Probably your solution can help me to do some trick here.
Your help on this will be appreciated.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Found a solution?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
scala solution:
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.entity.mime.{HttpMultipartMode, MultipartEntityBuilder}
import org.apache.http.impl.client.HttpClients
import org.apache.http.client.methods.{HttpPost}
@throws(classOf[Exception])
def addAttachmentToJiraTicket(jiraTicketId: String, attachmentPath: Path): Unit ={
val credential = new UsernamePasswordCredentials(AppConfig.instance.jiraMailId, AppConfig.instance.jiraToken)
val post = new HttpPost(myServer + "/rest/api/2/issue/" + jiraTicketId + "/attachments")
post.addHeader(new BasicScheme().authenticate(credential, post, null))
post.addHeader("X-Atlassian-Token", "no-check")
val entity = MultipartEntityBuilder.create()
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
entity.addPart("file", new FileBody(attachmentPath.toFile,ContentType.APPLICATION_OCTET_STREAM))
post.setEntity(entity.build())
val response = HttpClients.createDefault().execute(post)
val statusCode = response.getStatusLine.getStatusCode
if (statusCode != 200) {
throw new Exception(s"Invalid response status code:$statusCode")
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.