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,323,611
Community Members
 
Community Events
168
Community Groups

Attach file trough REST API

Hi,

I am trying to upload a file through jira's REST API. I am using my own java REST client based on jersey and json parser.

How do I construct json query for REST API?

REST resource: issue/"issueKey"/attachments

It should be something like in the following example: {"fields":{"attachment":{"filename":"filename"}}}


1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

It gave me a glue, but I still do not know how to implement it with jersey.

I have just found this article:http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/

Thanks a lot :)

So do you need any more help ?

Yes, I do need such help.

So, I created a servlet (tomcat7) with a html form, where users could create issues and upload files.

Part filePart = request.getPart("attachment_field");

String filename = getFilename(filePart);

String authBase64 = new String(Base64.encode(jiraUser+":"+jiraPass));

MultiPart multiPartInput = new MultiPart();

BodyPart bp = new BodyPart(filePart.getInputStream(), MediaType.APPLICATION_OCTET_STREAM_TYPE);

FormDataContentDisposition.FormDataContentDispositionBuilder dispositionBuilder = FormDataContentDisposition.name(FILE_ATTACHMENT_CONTROL_NAME);

dispositionBuilder.fileName(filename);

FormDataContentDisposition formDataContentDisposition = dispositionBuilder.build();

bp.setContentDisposition(formDataContentDisposition);

multiPartInput.bodyPart(bp);

Client clientAtt = Client.create();

WebResource attachmentsResource = clientAtt.resource("http://jira/jira/issue/"+issueKey+"/attachments");

WebResource.Builder builder = attachmentsResource.type(MultiPartMediaTypes.createFormData());

builder.header("Authorization", "Basic " + authBase64);

builder.header("X-Atlassian-Token", "nocheck");

builder.post(multiPartInput);

ERROR that I got:

com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.MultiPart, and MIME media type, multipart/form-data; boundary=Boundary_1_795431812_1395317246600, was not found

Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.MultiPart, and MIME media type, multipart/form-data; boundary=Boundary_1_795431812_1395317246600, was not found

......

My problem is that I do not know how to fix this.

Any suggestion is highly appreciated!

You can see complete example from the jira-rest-java-client - here's a link to the source code. You can checkout it and use it for reference. Use the addAttachment method as a starting point :).

https://bitbucket.org/atlassian/jira-rest-java-client/src/7bceaef5b27c36ac7e69e178e9178095034d62ca/core/src/main/java/com/atlassian/jira/rest/client/internal/async/AsynchronousIssueRestClient.java

The example is very good, but the client is based on other library. I want to use jersey.

I found this example - https://docs.atlassian.com/jira-rest-java-client/0.5-m6/xref/com/atlassian/jira/rest/client/internal/jersey/JerseyIssueRestClient.html

I am susspecting that I have to use earlier version of jersey-multipart jar, but at the moment I can not say something more.

Have you tried initializing the client as follows:

Client client = ClientBuilder.newBuilder()
            .register(MultiPartFeature.class)
            .build();

Which version of jeresey are you on ?

I managed to resolve the problem by adding some configuration to the client:

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
Client clientAtt = Client.create(cc);

Thank you for your help.

Great! If my answer helped you please mark it as accepted.

Thanks!

TAGS
Community showcase
Published in Apps & Integrations

4 Amazing Product and Project Management Apps compete for Best App Demo in July Appy Hours 🥳

Thanks so much to everyone who joined for our July Appy Hours! Product and project management presenter highlights  Checkout - Procurement and asset manager, presented by @Vincent Mutambuki ...

90 views 1 8
Read article

Atlassian Community Events