diff --git a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/BodyRepresentation.kt b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/BodyRepresentation.kt index 4cf791208..6513a9465 100644 --- a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/BodyRepresentation.kt +++ b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/BodyRepresentation.kt @@ -19,7 +19,7 @@ fun Body.representationOfBytes(contentType: String?): String { var charset = Charsets.UTF_8 val charsetGroup = TEXT_CONTENT_TYPE.find(actualContentType)!!.groupValues[1] if (charsetGroup.isNotEmpty()) { - val charsetName = charsetGroup.substringAfter('=').toUpperCase() + val charsetName = charsetGroup.substringAfter('=').substringBefore(';').toUpperCase() charset = Charset.forName(charsetName) } return String(toByteArray(), charset) diff --git a/fuel/src/test/kotlin/com/github/kittinunf/fuel/core/BodyRepresentationTest.kt b/fuel/src/test/kotlin/com/github/kittinunf/fuel/core/BodyRepresentationTest.kt index 4e2e09d2c..3338dc002 100644 --- a/fuel/src/test/kotlin/com/github/kittinunf/fuel/core/BodyRepresentationTest.kt +++ b/fuel/src/test/kotlin/com/github/kittinunf/fuel/core/BodyRepresentationTest.kt @@ -166,6 +166,26 @@ class BodyRepresentationTest : MockHttpTestCase() { } } + @Test + fun textRepresentationOfJsonWithUtf8AndOtherParameters() { + val contentTypes = listOf( + "application/json;charset=utf-8;api-version=5.1", + "application/json; charset=utf-8; api-version=5.1", + "application/json;charset=utf-8;api-version=5.1;test=true", + "application/json; charset=utf-8; api-version=5.1; test=true" + ) + val content = "{ \"foo\": 42 }" + + contentTypes.forEach { contentType -> + assertThat( + DefaultBody + .from({ ByteArrayInputStream(content.toByteArray()) }, { content.length.toLong() }) + .asString(contentType), + equalTo(content) + ) + } + } + @Test fun textRepresentationOfCsv() { val contentTypes = listOf("text/csv")