add an attachment to jira issue via rest api

tamil live July 29, 2017

Hi,

I have integrated with JIRA using OAuth 1.0.

so i have access token ,consumer key,oauth signature in my hand.

For attachment API we have to send a request with file(type as multipart form data)

is it possible to add an attachment to an issue using oauth 1.0 authentication method.

if it is possible means please share the code snippets here.

Thanks in advance.

3 answers

1 accepted

2 votes
Answer accepted
Peter Klimpt September 4, 2018

In Java:

  • Fetch the Example Sources as linked in the oauth examples (https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-oauth-authentication) and navigate to "<> Sources" and download them.
  • Make sure to add the referred google client libraries to your buildpath (For the below sample I've been using google-http-client-1.23.0.jar and google-oauth-client-1.22.0.jar)
  • create the Application Link and the keys as described in the tutorial
  • Add a new package 'OAuth' in the example project and add the class below.
  • adjust all 'public static final' Strings, endpointURL, filepath, jiraBaseURL accordingly to fit your environment

 

Note that this code is just a very blunt example, you may have to adjust some values as per your further requirements.

package OAuth;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

import com.atlassian.oauth.client.example.JiraOAuthGetAccessToken;
import com.atlassian.oauth.client.example.JiraOAuthTokenFactory;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpMediaType;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.MultipartContent;
import com.google.api.client.http.javanet.NetHttpTransport;

public class SampleAttachmentUpload {
private static String endpointurl= "http://localhost/rest/api/2/issue/TEST-1/attachments";

public static final String CONSUMER_KEY = "ConsumerKeyValue";
public static final String PRIVATE_KEY = "PrivateKeyValue";
public static final String ACCESS_TOKEN = "AccessTokenValue";
public static final String SECRET = "SecretValue";
public static final String JIRA_HOME = "http://localhost";

public static void createAttachmentRequest() throws Exception {
String filepath= "C:\\development\\jiraadapter\\sample.zip";
File initialFile = new File(filepath);
String fileName= initialFile.getName();
System.out.println("filename is: " + fileName );
InputStream inputStream = new FileInputStream(initialFile);


MultipartContent.Part part = new MultipartContent.Part()
.setContent(new InputStreamContent("application/gzip", inputStream))
.setHeaders(new HttpHeaders().set(
"Content-Disposition",
String.format("form-data; name=\"file\"; filename=\"%s\"", fileName) // TODO: escape fileName?
));
MultipartContent content = new MultipartContent()
.setMediaType(new HttpMediaType("multipart/form-data").setParameter("boundary", UUID.randomUUID().toString()))
.addPart(part);



String jiraBaseURL= "http://localhost";
JiraOAuthTokenFactory oAuthGetAccessTokenFactory = new JiraOAuthTokenFactory(jiraBaseURL);
JiraOAuthGetAccessToken oAuthAccessToken = oAuthGetAccessTokenFactory.getJiraOAuthGetAccessToken(ACCESS_TOKEN, SECRET, CONSUMER_KEY, PRIVATE_KEY);
oAuthAccessToken.verifier = SECRET;

OAuthParameters parameters= oAuthAccessToken.createParameters();

GenericUrl jiraUrl= new GenericUrl(endpointurl);

HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(parameters);
HttpRequest request = requestFactory.buildPostRequest(jiraUrl, content);
request.getHeaders().put("X-Atlassian-Token", "nocheck");
request.execute();
}
}
Artem Grotskyi December 12, 2018

Thank you, Peter!

You saved my time.

It works like a charm. :)

0 votes
Vasundara Devi Palatla February 5, 2019

Thanks it solved my issue

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 29, 2017

Suggest an answer

Log in or Sign up to answer