1818import org .springframework .hateoas .MediaTypes ;
1919import org .springframework .http .HttpMethod ;
2020import org .springframework .http .MediaType ;
21- import org .springframework .http . ResponseEntity ;
21+ import org .springframework .util . MultiValueMap ;
2222import org .springframework .web .client .RestClient ;
2323import org .springframework .web .client .RestClient .RequestBodySpec ;
2424import org .springframework .web .client .RestClient .RequestHeadersSpec ;
@@ -49,7 +49,7 @@ public HalFormsBodyRequest requestTemplate(@NonNull HalDocument document, @NonNu
4949 .contentType (contentType )
5050 .accept (MediaTypes .HAL_FORMS_JSON );
5151
52- return new DefaultHalFormsBodyRequest (bodySpec , template .getProperties ());
52+ return new DefaultHalFormsBodyRequest (bodySpec , contentType , template .getProperties ());
5353 }
5454
5555 public Optional <HalFormsTemplate > getTemplate (@ NonNull HalDocument document , @ NonNull String name ) {
@@ -99,6 +99,9 @@ static class DefaultHalFormsBodyRequest implements HalFormsBodyRequest {
9999 @ NonNull
100100 private final RequestBodySpec request ;
101101
102+ @ NonNull
103+ private final MediaType contentType ;
104+
102105 @ NonNull
103106 private final List <HalFormsProperty > properties ;
104107
@@ -118,8 +121,14 @@ public HalFormsBodyRequest properties(Function<HalFormsProperty, Object> valueFu
118121 body .put (property .name , valueFunction .apply (property ));
119122 }
120123
121- request .body (body , new ParameterizedTypeReference <Map <String , Object >>(){
122- });
124+ if (contentType .includes (MediaType .APPLICATION_FORM_URLENCODED ) || contentType .includes (MediaType .MULTIPART_FORM_DATA )) {
125+ // body must be a MultiValuedMap because the HttpMessageConverter only supports MultiValuedMap
126+ request .body (MultiValueMap .fromSingleValue (body ), new ParameterizedTypeReference <MultiValueMap <String , Object >>() {
127+ });
128+ } else {
129+ request .body (body , new ParameterizedTypeReference <Map <String , Object >>() {
130+ });
131+ }
123132
124133 return this ;
125134 }
0 commit comments