Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multipart/* MediaTypes in RestTemplate
Prior to this commit, RestTemplate posted multipart with Content-Type "multipart/form-data" even if the FormHttpMessageConverter configured in the RestTemplate had been configured to support additional multipart subtypes. This made it impossible to POST form data using a content type such as "multipart/mixed" or "multipart/related". This commit addresses this issue by updating FormHttpMessageConverter to support custom multipart subtypes for writing form data. For example, the following use case is now supported. MediaType multipartMixed = new MediaType("multipart", "mixed"); restTemplate.getMessageConverters().stream() .filter(FormHttpMessageConverter.class::isInstance) .map(FormHttpMessageConverter.class::cast) .findFirst() .orElseThrow(() -> new IllegalStateException("Failed to find FormHttpMessageConverter")) .addSupportedMediaTypes(multipartMixed); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("field 1", "value 1"); parts.add("file", new ClassPathResource("myFile.jpg")); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setContentType(multipartMixed); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(parts, requestHeaders); restTemplate.postForLocation("https://example.com/myFileUpload", requestEntity); Closes gh-23159
- Loading branch information
Showing
6 changed files
with
242 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.