From 1ada71cdcb19a2a6415e4acdc51ebc2c2cd1d05c Mon Sep 17 00:00:00 2001 From: Sunny Chung Date: Fri, 16 Feb 2024 21:11:08 +0800 Subject: [PATCH] fix default content-type header of JSON requests was wrong --- .../extension/UserRequestConversionExtension.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/extension/UserRequestConversionExtension.kt b/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/extension/UserRequestConversionExtension.kt index b08a8cd8..e8d40db3 100644 --- a/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/extension/UserRequestConversionExtension.kt +++ b/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/extension/UserRequestConversionExtension.kt @@ -29,6 +29,7 @@ import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.MediaType.Companion.toMediaType import okhttp3.Request import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder +import org.apache.hc.core5.http.ContentType import org.apache.hc.core5.http.message.BasicNameValuePair import org.apache.hc.core5.http.nio.AsyncRequestProducer import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers @@ -215,7 +216,13 @@ fun HttpRequest.toApacheHttpRequest(): Pair { *(entity.trailers?.get()?.toTypedArray() ?: emptyArray()) ) } - is StringBody -> AsyncEntityProducers.create(body.value, Charsets.UTF_8) + + is StringBody -> AsyncEntityProducers.create( + /* content = */ body.value, + /* contentType = */ contentType.headerValue?.let { ContentType.create(it, Charsets.UTF_8) } + ?: ContentType.DEFAULT_BINARY.withCharset(Charsets.UTF_8) + ) + null -> null else -> throw UnsupportedOperationException() }