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

Illés Ákos
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!
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

0 answers

Suggest an answer

Log in or Sign up to answer