Ahmed Lotfy
Ahmed Lotfy

Reputation: 1145

Post multipart (PDF) file using Spring RestTemplate

I'm trying to make a POST request to existing endpoint to upload a pdf document. I tried the below code and many other solutions but always getting 400 status code please help...

public String upload(String token, byte[] doc) throws Exception {

        String url = "http://localhost:8080/api/document";
        RestTemplate rest = new RestTemplate();
        MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        rest.getMessageConverters().add(new ByteArrayHttpMessageConverter());

        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.MULTIPART_FORM_DATA);
        header.add("X-Auth-Token", token);

        HttpHeaders bodyHeader = new HttpHeaders();
        bodyHeader.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();

        HttpHeaders jsonHeader = new HttpHeaders();
        HttpEntity<String> jsonHttpEntity = new HttpEntity<>("doc", jsonHeader);

        HttpHeaders fileHeader = new HttpHeaders();
        fileHeader.setContentType(MediaType.APPLICATION_PDF);


        HttpEntity<byte[]> fileEntity = new HttpEntity<>(doc, fileHeader);

        multipartRequest.add("", bodyHeader);
        multipartRequest.add("reference", jsonHttpEntity);
        multipartRequest.add("file", fileEntity);
        System.out.println("body:    " + multipartRequest.toString());
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequest, header);

        ResponseEntity<String> result = rest.exchange(url, HttpMethod.POST, requestEntity, String.class);

        return result.getBody();
    }

Upvotes: 1

Views: 4709

Answers (1)

Ahmed Lotfy
Ahmed Lotfy

Reputation: 1145

You can try to pass the file itself rather than the byte array byte[], i.e.

import java.io.File;
import java.io.IOException;

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

public class Application {
    public static void main(String[] args) throws IOException {
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        FileSystemResource value = new FileSystemResource(new File("D://test.pdf"));
        map.add("file", value);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.exchange("http://localhost:8080/api/document", HttpMethod.POST, requestEntity, String.class);
    }
}

Here is a reference for this code snippet

Upvotes: 2

Related Questions