add an attachment to jira issue via rest api


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.

2 answers

0 votes

In Java:

  • Fetch the Example Sources as linked in the oauth examples ( 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.util.UUID;

import com.atlassian.oauth.client.example.JiraOAuthGetAccessToken;
import com.atlassian.oauth.client.example.JiraOAuthTokenFactory;

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\\";
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(
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()))

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");

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

24,514 views 2 7
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you