Skip to content

Commit d48af2b

Browse files
committed
ACC-2402 support multipart/form-data in HalFormsClient
1 parent 2758aaf commit d48af2b

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/main/java/com/contentgrid/hateoas/client/hal/forms/DefaultHalFormsClient.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.springframework.hateoas.MediaTypes;
1919
import org.springframework.http.HttpMethod;
2020
import org.springframework.http.MediaType;
21-
import org.springframework.http.ResponseEntity;
21+
import org.springframework.util.MultiValueMap;
2222
import org.springframework.web.client.RestClient;
2323
import org.springframework.web.client.RestClient.RequestBodySpec;
2424
import 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

Comments
 (0)