Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,558,959
Community Members
 
Community Events
184
Community Groups

Unable to add attachment in Jira via rest api

I am attempting to upload a file to a Jira Issue via the rest api. I get a return code of 415 when I do. Below is my code snippet, any recommendations would be forthcoming:

 

String url = "https://" + serverName + "/rest/api/2/issue/" + issuenbr + "/attachments";
logger.debug("URL IS " + url);
boolean attachmentAdded = false;
BasicHeader contenttype = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
BasicHeader jiraheader = new BasicHeader("X-Atlassian-Token", "no-check");
List<BasicHeader> headers = new ArrayList<BasicHeader>();
headers.add(contenttype);
headers.add(jiraheader);
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(sys_username, sys_password)
);

try (CloseableHttpClient httpclient = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.setDefaultHeaders(headers)
.build();) {
File file = new File(fileuri);
String message = file.getName();

// build multipart upload request,modes: HttpMultipartMode.BROWSER_COMPATIBLE
HttpEntity data = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532)
.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, file.getName())
.addTextBody("text", message, ContentType.DEFAULT_BINARY).build();

// build http request and assign multipart upload data
HttpUriRequest request = RequestBuilder.post(url).setEntity(data).build();

CloseableHttpResponse response = httpclient.execute(request); {

// 401 if wrong user/password
System.out.println(response.getStatusLine().getStatusCode());

HttpEntity entity = response.getEntity();
if (entity != null) {
// return it as a String
String result = EntityUtils.toString(entity);
System.out.println("Response::" + result);
}

}

1 answer

1 accepted

0 votes
Answer accepted

I implemented the authorization header improperly, this works:

 

BasicHeader authHeader = new BasicHeader("Authorization", getBasicAuthenticationHeader(sys_username, sys_password));
List<BasicHeader> headers = new ArrayList<BasicHeader>();
headers.add(jiraheader);
headers. Add(authHeader);

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.10
TAGS
AUG Leaders

Atlassian Community Events