Search code examples
springjirazipresttemplatejira-rest-api

JIRA REST API corrupts uploaded zip attachment


I am attempting to use the JIRA Rest API (version 2) to add a zip attachment to an issue.

The code seems to work fine, but when I look at the attached files on the ticket the attachment is almost twice the size it should be, and upon attempting to open it I find it is corrupt.

I can upload an image file (a png in my testing) without an issue.

The code I am using is:

String basic = createBasicAuth();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + basic);
headers.add("X-Atlassian-Token", "no-check");
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

LinkedMultiValueMap<String, Resource> map = new LinkedMultiValueMap<>();
map.add("file", new ClassPathResource("/zipfile.zip"));

HttpEntity<LinkedMultiValueMap<String, Resource>> body = new HttpEntity<>(map, headers);

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(jiraUrl, body, String.class);

I have attempted to use a ByteArrayResource rather than ClassPathResource but this results in a 500 error from the server.

I have also attempted to wrap each resource in their own HttpEntity, given the zip file its own headers with the media type set to application/octet-stream.

Has anybody else had this issue? If not I will log it with Atlassian. Any help would be great.

Thanks


Solution

  • Turns out this is an issue wither with the Spring (3.2.1) RestTemplate, or how I am using it.
    Using Postman manually upload the zip file results in a successfully uploaded zip file.