add an attachment to jira issue via rest api

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.

2 answers

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();
}
}

Thank you, Peter!

You saved my time.

It works like a charm. :)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,097 views 4 9
Read article

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