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"}}}
Community moderators have prevented the ability to post new answers.
See that discussion, it should answer your question - https://answers.atlassian.com/questions/249347/how-do-i-upload-attachment-to-jira-issue-via-rest-api
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So do you need any more help ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have you tried initializing the client as follows:
Client client = ClientBuilder.newBuilder() .register(MultiPartFeature.class) .build();
Which version of jeresey are you on ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great! If my answer helped you please mark it as accepted.
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.