Last active
February 6, 2024 21:19
-
-
Save stefan-huettemann/cc6025bf40208b67bd2d18d6d0599e69 to your computer and use it in GitHub Desktop.
Multipart Request using spring RestTemplate
This file contains hidden or 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
protected ResponseEntity<Resource<List<Attachment>>> patchMultipartApiV1_Treatments_TID(final String aTID, | |
final Treatment aTreatment, | |
final boolean keepLock, | |
final MultipartFile... aMultipartFiles) throws IOException { | |
final MultiValueMap<String, Object> theMultipartRequest = new LinkedMultiValueMap<>(); | |
// creating an HttpEntity for the JSON part: | |
final HttpHeaders theJsonHeader = new HttpHeaders(); | |
theJsonHeader.setContentType(MediaType.APPLICATION_JSON); | |
theMultipartRequest.add("treatment", new HttpEntity<>(aTreatment, theJsonHeader)); | |
// creating an HttpEntity for the file part: | |
if (aMultipartFiles != null) { | |
for (final MultipartFile theMultipartFile : aMultipartFiles) { | |
final HttpHeaders theFileHeader = new HttpHeaders(); | |
if (StringUtils.hasText(theMultipartFile.getContentType())) { | |
theFileHeader.add(HttpHeaders.CONTENT_TYPE, theMultipartFile.getContentType()); | |
} else { | |
theFileHeader.setContentType(MediaType.APPLICATION_OCTET_STREAM); | |
} | |
theFileHeader.add(HttpHeaders.CONTENT_DISPOSITION, String.format("form-data; name=\"file\"; filename=\"%s\"", theMultipartFile.getName())); | |
theMultipartRequest.add("file", new HttpEntity<>(theMultipartFile.getBytes(), theFileHeader)); | |
} | |
} | |
final HttpHeaders theMultipartHeaders = headers(); | |
theMultipartHeaders.set(properties.getSecurity().getApiKeyName(), properties.getSecurity().getApiKey()); | |
theMultipartHeaders.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE); | |
theMultipartHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE); | |
final HttpEntity<MultiValueMap<String, Object>> theHttpEntity = new HttpEntity<>(theMultipartRequest, theMultipartHeaders); | |
final String theUrlTemplate = AbstractV1Endpoint.API_V1 + (keepLock ? TreatmentsEndpoint.PATCH_TREATMENTS_TID + "?keepLock=true" : TreatmentsEndpoint.PATCH_TREATMENTS_TID); | |
final ParameterizedTypeReference<Resource<List<Attachment>>> aTypeReference = new ParameterizedTypeReference<Resource<List<Attachment>>>() {}; | |
return testRestTemplate().exchange(theUrlTemplate, HttpMethod.PATCH, theHttpEntity, aTypeReference, aTID); | |
} |
What format will do
It's String.format
THANK YOU SOOOOOO MUCH. I was searching the whole internet for a working solution for my problem and you just gave it to me :)
Thanks a lot - solved a few things on my end.
If anybody stumbles upon this to send data from an InputStream, use
theMultipartRequest.add("file", new HttpEntity<>(new InputStreamResource(INPUTSTREAM), theFileHeader));
Thanks @stefan-huettemann
The secret sauce for me was...
theFileHeader.add(HttpHeaders.CONTENT_DISPOSITION, String.format("form-data; name=\"file\"; filename=\"%s\"", theMultipartFile.getName()));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What format will do