@@ -9,6 +9,7 @@ import io.ktor.request.*
9
9
import io.ktor.response.*
10
10
import io.ktor.routing.*
11
11
import io.ktor.util.*
12
+ import java.nio.charset.Charset
12
13
import kotlinx.coroutines.coroutineScope
13
14
import kotlinx.serialization.json.*
14
15
import kotlinx.serialization.json.Json.Default.decodeFromString
@@ -56,7 +57,8 @@ class GraphQL(val schema: Schema) {
56
57
val routing: Route .() -> Unit = {
57
58
route(config.endpoint) {
58
59
post {
59
- val request = decodeFromString(GraphqlRequest .serializer(), call.receiveText())
60
+ val bodyAsText = call.receiveTextWithCorrectEncoding()
61
+ val request = decodeFromString(GraphqlRequest .serializer(), bodyAsText)
60
62
val ctx = context {
61
63
config.contextSetup?.invoke(this , call)
62
64
}
@@ -90,6 +92,17 @@ class GraphQL(val schema: Schema) {
90
92
return GraphQL (schema)
91
93
}
92
94
95
+ private suspend fun ApplicationCall.receiveTextWithCorrectEncoding (): String {
96
+ fun ContentType.defaultCharset (): Charset = when (this ) {
97
+ ContentType .Application .Json -> Charsets .UTF_8
98
+ else -> Charsets .ISO_8859_1
99
+ }
100
+
101
+ val contentType = request.contentType()
102
+ val suitableCharset = contentType.charset() ? : contentType.defaultCharset()
103
+ return receiveStream().bufferedReader(charset = suitableCharset).readText()
104
+ }
105
+
93
106
private fun GraphQLError.serialize (): String = buildJsonObject {
94
107
put(" errors" , buildJsonArray {
95
108
addJsonObject {
0 commit comments