-
Notifications
You must be signed in to change notification settings - Fork 38k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multipart/related and multipart/mixed MediaTypes in RestTemplate #23159
Comments
What happens if you configure explicit support for those content types as follows? restTemplate.getMessageConverters().stream()
.filter(FormHttpMessageConverter.class::isInstance)
.map(FormHttpMessageConverter.class::cast)
.findFirst()
.ifPresent(converter -> {
List<MediaType> supportedMediaTypes = new ArrayList<>(converter.getSupportedMediaTypes());
supportedMediaTypes.add(new MediaType("multipart", "mixed"));
supportedMediaTypes.add(new MediaType("multipart", "related"));
converter.setSupportedMediaTypes(supportedMediaTypes);
}); |
That was our first inclination; however, it didn't work because of another check in the Lines 279 to 291 in da582da
The |
Thanks for the feedback.
@rstoyanchev, what do you think about making changes along those lines? Also, what do you think about introducing |
As far as I can see, there are a couple of checks against The checks could probably be 1) relaxed to check only the type ("multipart"), 2) or changed to explicitly check all 3 (i.e. form-data, mixed, related), or 3) invert the check, i.e. that it's not Likewise for setting the multipart media type on the Taking both the checks and the setting of headers together, it's probably best to use the |
Thanks for the feedback, @rstoyanchev. I'll look into implementing something along those lines. |
Thanks to #23203, it's now a little easier to register custom supported restTemplate.getMessageConverters().stream()
.filter(FormHttpMessageConverter.class::isInstance)
.map(FormHttpMessageConverter.class::cast)
.findFirst()
.orElseThrow(() -> new IllegalStateException("Failed to find FormHttpMessageConverter"))
.addSupportedMediaTypes(new MediaType("multipart", "mixed")); |
Current work on this issue can be viewed in my feature branch: https://github.com/sbrannen/spring-framework/commits/issues/gh-23159-RestTemplate-multipart |
For the check in
I've done this slightly differently in my feature branch. The code now honors the If the user doesn't supply an explicit content type — for example, by invoking This works fine if the user has properly added the desired @rstoyanchev, do you think that is OK like that? Or do feel strongly about transparently picking the current content type from the configured set of supported types? I'm a little hesitant to do the latter, b/c that makes it a bit of a guessing game for the user. Until now, we have only supported Or am I missing something? |
Looks good overall although there is one more explicit check against MULTIPART_FORM_DATA that should probably also be generalized. |
Thanks for the review.
Indeed. Good catch! I've addressed that locally as follows and added appropriate tests that I'll be pushing later. for (MediaType supportedMediaType : getSupportedMediaTypes()) {
if (MULTIPART_ALL.includes(supportedMediaType)) {
// We can't read multipart, so skip this supported media type.
continue;
}
if (supportedMediaType.includes(mediaType)) {
return true;
}
} |
This support has been added in 5008423. @sethcleveland, feel free to try it out in the next 5.2 snapshot and let us know if it works for you. |
See also: #23209 |
Affects: <v5.2.0.M3>
At the moment, the
RestTemplate
posts multipart withContent-Type: multipart/form-data
.We have a requirement to post multipart data with a
Content-Type
ofmultipart/mixed
ormultipart/related
.C# provides flexibility for this with a
MultipartContent
with a subtype parameter.https://docs.microsoft.com/en-us/dotnet/api/system.net.http.multipartcontent.-ctor?view=netframework-4.8#System_Net_Http_MultipartContent__ctor_System_String_
Any recommendations?
The text was updated successfully, but these errors were encountered: