Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Attach file trough REST API

Stefan Dinev March 17, 2014

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

0 votes
Answer accepted
Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2014
Stefan Dinev March 17, 2014

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 :)

Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2014

So do you need any more help ?

Stefan Dinev March 19, 2014

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!

Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 19, 2014

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

Stefan Dinev March 19, 2014

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.

Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 19, 2014

Have you tried initializing the client as follows:

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

Which version of jeresey are you on ?

Stefan Dinev March 21, 2014

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.

Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 21, 2014

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

Thanks!

TAGS
AUG Leaders

Atlassian Community Events