diff --git a/rest-client-base/src/main/java/com/wultra/core/rest/client/base/DefaultRestClient.java b/rest-client-base/src/main/java/com/wultra/core/rest/client/base/DefaultRestClient.java index befdb3c..3ebdb10 100644 --- a/rest-client-base/src/main/java/com/wultra/core/rest/client/base/DefaultRestClient.java +++ b/rest-client-base/src/main/java/com/wultra/core/rest/client/base/DefaultRestClient.java @@ -363,7 +363,7 @@ public ResponseEntity post(String path, Object request, MultiValueMap handleResponse(rs, responseType)) @@ -398,7 +398,7 @@ public void postNonBlocking(String path, Object request, MultiValueMap handleResponse(rs, responseType)) @@ -444,7 +444,7 @@ public ResponseEntity put(String path, Object request, MultiValueMap handleResponse(rs, responseType)) @@ -472,7 +472,7 @@ public void putNonBlocking(String path, Object request, MultiValueMap handleResponse(rs, responseType)) @@ -586,7 +586,7 @@ public ResponseEntity patch(String path, Object request, MultiValueMap handleResponse(rs, responseType)) @@ -614,7 +614,7 @@ public void patchNonBlocking(String path, Object request, MultiValueMap handleResponse(rs, responseType)) @@ -770,6 +770,15 @@ private Mono> handleResponse(ClientResponse response, Para }); } + private static MediaType resolveContentType(final RestClientConfiguration config, final MultiValueMap headers) { + if (headers != null && headers.containsKey(HttpHeaders.CONTENT_TYPE)) { + final MediaType contentType = MediaType.valueOf(headers.getFirst(HttpHeaders.CONTENT_TYPE)); + logger.debug("Overriding content type {} from config by {} from the given headers", config.getContentType(), contentType); + return contentType; + } + return config.getContentType(); + } + /** * In case base URL is specified, append the path to complete the URL. Otherwise use the path as the URL specification. * @param uriSpec Request headers URI specification. diff --git a/rest-client-base/src/test/java/com/wultra/core/rest/client/base/DefaultRestClientTest.java b/rest-client-base/src/test/java/com/wultra/core/rest/client/base/DefaultRestClientTest.java index 3e872fa..d19db5f 100644 --- a/rest-client-base/src/test/java/com/wultra/core/rest/client/base/DefaultRestClientTest.java +++ b/rest-client-base/src/test/java/com/wultra/core/rest/client/base/DefaultRestClientTest.java @@ -662,6 +662,23 @@ void testPostWithMultipartData() throws RestClientException { assertEquals(requestData, responseEntity.getBody().getResponseObject().getResponse()); } + @Test + void testPostOctetStream() throws Exception { + final byte[] request = {1, 2}; + + final HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); + + final ResponseEntity> responseEntity = + restClient.post("/octet-stream", request, null, headers, new ParameterizedTypeReference<>(){}); + + assertNotNull(responseEntity); + assertNotNull(responseEntity.getBody()); + assertNotNull(responseEntity.getBody().getResponseObject()); + assertEquals("OK", responseEntity.getBody().getStatus()); + assertEquals("length: 2", responseEntity.getBody().getResponseObject().getResponse()); + } + @Test void testPostWithLargeServerResponse() { final Logger defaultRestClientLogger = (Logger) LoggerFactory.getLogger(DefaultRestClient.class); diff --git a/rest-client-base/src/test/java/com/wultra/core/rest/client/base/controller/PublicTestRestController.java b/rest-client-base/src/test/java/com/wultra/core/rest/client/base/controller/PublicTestRestController.java index 874f6c1..b2c8ad4 100644 --- a/rest-client-base/src/test/java/com/wultra/core/rest/client/base/controller/PublicTestRestController.java +++ b/rest-client-base/src/test/java/com/wultra/core/rest/client/base/controller/PublicTestRestController.java @@ -21,13 +21,13 @@ import io.getlime.core.rest.model.base.request.ObjectRequest; import io.getlime.core.rest.model.base.response.ObjectResponse; import io.getlime.core.rest.model.base.response.Response; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; import java.net.URI; import java.util.Arrays; import java.util.Enumeration; @@ -85,6 +85,12 @@ public ObjectResponse testPostWithMultipartRequestAndResponse(@Req return new ObjectResponse<>(testResponse); } + @PostMapping(value = "/octet-stream", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE) + public ObjectResponse testPostOctetStream(@RequestBody byte[] request) { + final TestResponse testResponse = new TestResponse("length: " + request.length); + return new ObjectResponse<>(testResponse); + } + @PostMapping("/object-response-large") public ObjectResponse testPostWithLargeServerResponse() { TestResponse testResponse = new TestResponse(Arrays.toString(new byte[10 * 1024 * 1024]));