Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

"No attachments found" exception when trying to upload file through API

Marko Praprotnik
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 1, 2018

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.

1 answer

1 accepted

1 vote
Answer accepted
Marko Praprotnik
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 2, 2018

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:

https://github.com/marcosalis/kraken/blob/master/kraken_lib/src/main/java/com/google/api/client/http/MultipartFormDataContent.java

It's written for Android, but super easy to adapt to common java projects.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events