Forums

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

Uploading file using Java gives 200 response, but no file was uploaded

Illés Ákos
Contributor
February 27, 2024

Hi all!

I'm trying to upload a pdf file to one of my Jira issues using Java. I tried older solutions posted on this forum, but they doesn't seem to work. Here is what I have now:

public CloseableHttpResponse addAttachment(String baseUrl, String issueKey, String filePath) throws IOException{
try (CloseableHttpClient client = HttpClientBuilder.create().build();) {
HttpPost httpPost = new HttpPost(baseUrl+"/"+issueKey+"/attachments");
httpPost.setHeader(new BasicHeader("Authorization", "Bearer " + apiKey));
httpPost.setHeader("X-Atlassian-Token", "no-check");
httpPost.setHeader("Content-Type", "multipart/form-data;boundary=gc0p4Jq0M2Yt08jU534c0p");

File file = new File(filePath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());

HttpEntity entity = builder.build();

httpPost.setEntity(entity);

CloseableHttpResponse response = client.execute(httpPost);

return response;
}
}

My problem is that it sends the request and responds with 200, but with a body of: [], and nothing is uploaded. Everything works fine with Postman, so it must be something with my code.

Someone please help me fix it.

Thanks,
Ákos

1 answer

1 accepted

0 votes
Answer accepted
Illés Ákos
Contributor
April 19, 2024

I couldn't solve the problem, but found a workaround. If I use a curl command from the code then it works. 
For anyone, who has the same problem here is my code:

public String addAttachment(String issueKey, String filePath) throws IOException {
//Command to execute
String[] command = {
"curl",
"-X", "POST",
"-H", "Authorization: Bearer " + apiKey,
"-H", "X-Atlassian-Token: no-check",
"-F", "file=@"+filePath,
baseUrl + "/issue/" + issueKey + "/attachments"
};

// Create ProcessBuilder
ProcessBuilder processBuilder = new ProcessBuilder(command);

// Start the process
Process process = processBuilder.start();

// Read the output
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String respond = "";
String line;
while ((line = reader.readLine()) != null) {
respond = respond.concat(line);
}
return respond;
}

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, team ’25 europe, early bird extension, conference discount, barcelona, hybrid event, AI teamwork, early bird pass, digital access, collaboration conference, atlassian events, register now, developer conference

1 week left to save on your Team '25 Europe pass

Still thinking about joining us for Team '25 Europe? Early bird has been extended for just one week! Now’s the time to lock in your lowest rate. Use code TEU25COMM-20 to save 20% at checkout. Hurry, this only lasts for 1 more week.

Learn more
AUG Leaders

Atlassian Community Events