I'm trying to post Atom xml and file with multipart/related request using RestTemplate. The question is - is it possible to change headers of parts for example Content-Type presented after boundary in atom part or add Content-ID in file part or how to properly create post request in this case. My request should look like this:
POST /app/psw HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: localhost
Accept: */*
Authorization: Basic YWdzOmFnczEyMw==
Content-Type: multipart/related;boundary===9B752C681081408==;type=application/atom+xml
Content-Length: 7019
Expect: 100-continue
--==9B752C681081408==
Content-Type: application/atom+xml
<?xml version="1.0" encoding="utf-8"?>
<atom:entry ...>
...
</atom:entry>
--==9B752C681081408==
Content-Type: video/mp2t
Content-ID: <prod@example.com>
123f3242e34...binary data...12313ed
--==9B752C681081408==--
I must use the RestTemplate or Spring WebClient.
For now it looks like presented below, but part with atom has Content-Type: application/xml instead of application/atom+xml
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().stream()
.filter(FormHttpMessageConverter.class::isInstance)
.map(FormHttpMessageConverter.class::cast)
.findFirst()
.ifPresent(formHttpMessageConverter -> {
List<MediaType> supportedMediaTypes = new ArrayList<>(formHttpMessageConverter.getSupportedMediaTypes());
supportedMediaTypes.add(new MediaType("multipart","related"));
formHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
});
ResponseEntity<String> response;
LinkedMultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
map.add("atom",e); //e is xml object created with javax.xml.bind package
map.add("file",new FileSystemResource(file));
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","multipart/related;type=\"application/atom+xml\"");
HttpEntity<LinkedMultiValueMap<String,Object>> request = new HttpEntity<>(map,headers);
response = restTemplate.postForEntity(url,request,String.class);
Thank you in advance
Ok, I found solution which works for me. I will try to explain step by step how i did it.
private RestTemplate prepareRestTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
RestTemplate template = new RestTemplate(requestFactory);
template.getMessageConverters().stream()
.filter(FormHttpMessageConverter.class::isInstance)
.map(FormHttpMessageConverter.class::cast)
.findFirst()
.ifPresent(formHttpMessageConverter -> {
List<MediaType> supportedMediaTypes = new ArrayList<>(formHttpMessageConverter.getSupportedMediaTypes());
supportedMediaTypes.add(new MediaType("multipart", "related"));
formHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
});
return template;
}
private HttpHeaders createHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/related;type=\"application/atom+xml\"");
headers.setBasicAuth(properties.getProperty("service.login"), properties.getProperty("service.password"));
return headers;
}
private HttpEntity<String> createAtomPart(String xml) {
MultiValueMap<String, String> atomMap = new LinkedMultiValueMap<>();
atomMap.add(HttpHeaders.CONTENT_TYPE, "application/atom+xml");
return new HttpEntity<>(xml, atomMap);
}
private HttpEntity<InputStreamResource> createFilePart(InputStream file, String contentId, String contentType) {
MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
fileMap.add(HttpHeaders.CONTENT_TYPE, contentType);
fileMap.add("Content-ID", contentId);
return new HttpEntity<>(new InputStreamResource(file), fileMap);
}
private HttpEntity<MultiValueMap<String, Object>> prepareRequest(InputStream file, String xml, String contentId, String contentType) {
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("atom", createAtomPart(xml));
bodyMap.add("file", createFilePart(file, contentId, contentType));
return new HttpEntity<>(bodyMap, createHeaders());
}
public ResponseEntity<String> sendPostRequest(InputStream file, String xml, String contentId, String contentType) throws ClientException {
HttpEntity<MultiValueMap<String, Object>> request = prepareRequest(file, xml, contentId, contentType);
ResponseEntity<String> response;
try {
response = restTemplate.postForEntity(uri, request, String.class);
} catch (HttpServerErrorException e) {
log.info("Error occurred on server side, reason:", e);
return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode());
} catch (HttpClientErrorException e) {
throw new ClientException(e.getStatusCode(), e.getResponseBodyAsString(), e);
}
return response;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.