javaspringresttemplateclam

clamav-rest Spring 3.2 RestTemplate results in 400 Bad Request


I'm trying to send a file to a Spring Boot application clamav-rest. The curl command is working fine. Here's the output,

curl -k -F name="test file" -F file=@"myfile.pdf" https://clamav.my.org/scan
POST /scan HTTP/1.1
Host: clamav.my.org
User-Agent: curl/7.56.1
Accept: */*
Content-Length: 22834
Content-Type: multipart/form-data; boundary=------------------------5814453ea3a5f3e0
Expect: 100-continue
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Thu, 18 Jan 2018 23:19:10 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 21
Everything ok : true

I'm using Spring 3.2 to make a REST call, which results in a 400 Bad Request error.

MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("name", "fileUpload.obj");
parts.add("file", in);

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

HttpEntity<MultiValueMap<String, Object>> requestEntity = 
    new HttpEntity<MultiValueMap<String, Object>>(parts, headers);

ResponseEntity<String> responseEntity = 
    restTemplate.exchange("https://clamav.my.org/scan", HttpMethod.POST, requestEntity, String.class);
String body = (String) responseEntity.getBody();

Here's the log output

Created POST request for "https://clamav.my.org/scan"
Setting request Accept header to [text/plain, application/json, application/*+json, */*]
Writing [{name=[fileUpload.obj], file=[[B@fe97ef91]}] as "multipart/form-data" using [org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@4885d075]
POST request for "https://clamav.my.org/scan" resulted in 400 (Bad Request); invoking error handler
Created POST request for "https://clamav.my.org/scan"
Setting request Accept header to [text/plain, application/json, application/*+json, */*]
Writing [{file=[[B@fe97ef91]}] using [org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@e8e8b3fa]
POST request for "https://clamav.my.org/scan" resulted in 400 (Bad Request); invoking error handler

I've tried several iterations and searched all over for example code. Any thoughts about how to make a successful request? TIA


Solution

  • Found the answer in a related post, sending-file-over-spring-rest-service-via-resttemplate

    The RestTemplate call is now,

            RestTemplate restTemplate = new RestTemplate();
            FormHttpMessageConverter converter = new FormHttpMessageConverter();
            restTemplate.getMessageConverters().add(converter);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    
            MultipartByteArrayResource resource = new MultipartByteArrayResource(in);
            resource.setFilename("fileUpload.obj");
    
            MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
            parts.add("name", "fileUpload.obj");
            parts.add("file", resource);
    
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(parts,
                    headers);
    
            ResponseEntity<String> responseEntity = restTemplate.exchange("https://clamav.my.org/scan",
                    HttpMethod.POST, requestEntity, String.class);
            String body = (String) responseEntity.getBody();
    

    which I'm sure can be improved, but it's working for now.