Hi!
I can't seem to figure out what I'm doing wrong when POST-ing a file to my JIRA Service Desk Server (v 3.13.0 on JIRA v 7.10.0).
I followed the tutorial on setting up OAuth and cloned the accompanying code from Bitbucket. This example code is the basis of my interaction with JIRA API and I can successfully create issues on my Desk through REST using OAuth.
Now I want to upload temporary files and than associate those files with issues, as described in the documentation. I'm trying to use the same google http-client as when creating the issue, but I'm getting the following exception when trying to upload a file:
com.google.api.client.http.HttpResponseException: 400
{"errorMessage":"No attachments found.","i18nErrorMessage":{"i18nKey":"sd.attachment.empty.error","parameters":[]}}
Here is my code (somewhat messy after numerous trial & error attempts):
// Upload the file as a multipart form
MultipartContent content = new MultipartContent();
FileContent partContent = new FileContent(URLConnection.guessContentTypeFromName(myFile.getName()), myFile);
HttpHeaders partHeaders = new HttpHeaders();
partHeaders.set("Content-Disposition", "form-data; Name=\"file\" FileName=\"" + fileName + "\"");
partHeaders.setContentType(contentType);
MultipartContent.Part part = new MultipartContent.Part(partHeaders, partContent);
content.addPart(part);
// Create the request using HttpRequestFactory (which is handing OAuth)
HttpRequest uploadRequest = requestFactory.buildPostRequest(new GenericUrl(uploadUrl), content);
uploadRequest.getHeaders().setContentType("multipart/form-data; boundary="+MULTIPART_BOUNDARY);
uploadRequest.getHeaders().set("Origin", "http://my.origin.com");
uploadRequest.getHeaders().set("Referer", "http://my.referer.com");
uploadRequest.getHeaders().set("X-Atlassian-Token", "no-check");
uploadRequest.getHeaders().set("X-ExperimentalApi", "opt-in");
// "No attachments found" when executing the request
HttpResponse uploadResponse = uploadRequest.execute();
Attaching files through web UI works.
Ok, I solved my issue. It seems that Google Java HttpClient does not follow the RFC 2388 specification. I used the following file as MultipartContent:
It's written for Android, but super easy to adapt to common java projects.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.