diff --git a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulRequest.java b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulRequest.java index 2aafa4e308f..203bb3d86d3 100644 --- a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulRequest.java +++ b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulRequest.java @@ -26,8 +26,10 @@ import ca.uhn.fhir.rest.client.api.IHttpResponse; import ca.uhn.fhir.util.StopWatch; import okhttp3.Call; import okhttp3.Call.Factory; +import okhttp3.MediaType; import okhttp3.Request; import okhttp3.RequestBody; +import okio.Buffer; import java.io.IOException; import java.util.Collections; @@ -80,9 +82,16 @@ public class OkHttpRestfulRequest extends BaseHttpRequest implements IHttpReques } @Override - public String getRequestBodyFromStream() { - // returning null to indicate this is not supported, as documented in IHttpRequest's contract - return null; + public String getRequestBodyFromStream() throws IOException { + if (myRequestBody == null) { + return null; + } + final Buffer buffer = new Buffer(); + myRequestBody.writeTo(buffer); + MediaType contentType = myRequestBody.contentType(); + return contentType == null || contentType.charset() == null + ? buffer.readUtf8() + : buffer.readString(contentType.charset()); } @Override diff --git a/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulRequestTest.java b/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulRequestTest.java index d62a99d96f2..a95e83a6316 100644 --- a/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulRequestTest.java +++ b/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulRequestTest.java @@ -1,12 +1,19 @@ package ca.uhn.fhir.okhttp.client; import ca.uhn.fhir.rest.api.RequestTypeEnum; +import okhttp3.MediaType; +import okhttp3.RequestBody; import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + import static org.junit.jupiter.api.Assertions.assertEquals; public class OkHttpRestfulRequestTest { + private final String ENTITY_CONTENT = "Some entity with special characters: é"; + @Test void toString_afterCreation_GetUsefulDataForLogging() { String theUrl = "https://example.com/fhir/meta"; @@ -24,4 +31,22 @@ public class OkHttpRestfulRequestTest { OkHttpRestfulRequest okHttpRestfulRequest = new OkHttpRestfulRequest(clientFactory.getNativeClient(), theUrl, RequestTypeEnum.POST, null); assertEquals("POST https://another.example.com/fhir/Task", okHttpRestfulRequest.toString()); } + + @Test + public void testGetRequestBodyFromStream() throws IOException { + RequestBody requestBody = RequestBody.create(ENTITY_CONTENT.getBytes(StandardCharsets.ISO_8859_1), MediaType.parse("text/plain; charset=ISO-8859-1")); + + String result = new OkHttpRestfulRequest(null, "https://test.com", null, requestBody).getRequestBodyFromStream(); + + assertEquals(ENTITY_CONTENT, result); + } + + @Test + public void testGetRequestBodyFromStreamWithUnknownCharset() throws IOException { + RequestBody requestBody = RequestBody.create(ENTITY_CONTENT.getBytes(StandardCharsets.UTF_8), MediaType.parse("text/plain; charset=UTF-8")); + + String result = new OkHttpRestfulRequest(null, "https://test.com", null, requestBody).getRequestBodyFromStream(); + + assertEquals(ENTITY_CONTENT, result); + } }