How to upload files to existing JIRA tickets via REST API

I managed to create new JIRA tickets using REST API but I’m having trouble uploading files to tickets that already exist. I keep running into errors when trying to attach screenshots or documents to my JIRA tickets.

Here’s my attempt at uploading a PNG file to an existing JIRA ticket:

private static String uploadFile(String filePath) throws URISyntaxException, FileNotFoundException {
    final java.net.URI serverUri = new java.net.URI("https://mycompany.atlassian.net");
    FileInputStream inputStream = new FileInputStream(filePath);
    JerseyJiraRestClientFactory clientFactory = new JerseyJiraRestClientFactory();
    NullProgressMonitor monitor = new NullProgressMonitor();
    
    JiraRestClient client = clientFactory.createWithBasicHttpAuthentication(serverUri, "testuser", "password123");
    Issue ticket = client.getIssueClient().getIssue(ticketKey, monitor);
    final java.net.URI uploadUri = new java.net.URI(serverUri + "/rest/api/2/issue/" + ticketKey + "/attachments");
    
    client.getIssueClient().addAttachment(monitor, uploadUri, inputStream, fileName);
    return filePath;
}

First I got this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpConnectionManager

After adding the missing JAR files I’m now getting:

com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java class com.sun.jersey.multipart.MultiPart was not found

Any ideas on how to fix this attachment upload issue?

Your code has several issues beyond the dependency problems. You’re passing wrong parameters to addAttachment - it wants the attachment URI from issue metadata, not one you build manually. Use client.getIssueClient().addAttachment(monitor, ticket.getAttachmentsUri(), inputStream, fileName) instead. Don’t close the FileInputStream before upload finishes or you’ll get random failures. I wrap file operations in try-with-resources to avoid resource leaks. Watch out for file size limits too - JIRA admins often restrict attachment sizes but the error messages don’t make this clear. If you’re still getting dependency errors after adding jersey-multipart, check that all your Jersey versions match. Mismatched versions cause weird runtime exceptions.

The MultiPart error indicates that you’re missing Jersey multipart dependencies. I encountered a similar issue previously with JIRA attachments. Ensure you include the jersey-multipart JAR in your classpath along with mimepull and other necessary dependencies. Additionally, there’s another issue: you’re manually constructing the upload URI rather than using the appropriate client method. The addAttachment method requires different parameters than those you’re providing. Instead, use the attachment URI retrieved from the issue object directly. Lastly, verify your JIRA permissions to confirm that your API user has the ‘Create Attachments’ permission for that project; I’ve witnessed cases where working code failed due to insufficient permissions, leading to unexpected upload errors.

Had this exact problem last week lol. You’re using the wrong method signature - addAttachment expects InputStream, filename and optional description but you’re passing URI first. Try client.getIssueClient().addAttachment(monitor, ticket.getAttachmentsUri(), inputStream, "screenshot.png") without building a custom URI. Also make sure to flush the inputstream before closing it or the upload might fail randomly.