https://kotlinlang.org logo
#ktor
Title
# ktor
l

lenqnr

07/01/2019, 6:01 AM
When I send a POST request with String typed body,
Copy code
<http://client.post|client.post><String>("/path", "hello")
I get double-quoted data on both sides.
Copy code
println(call.receiveText())
// Result: "hello"
What is the proper way to handle this?
c

cy

07/01/2019, 4:14 PM
It looks like you are using json serializer for sending plain text data
// cc @e5l
l

lenqnr

07/01/2019, 11:43 PM
Yes, I sent a serialized object using installed json feature and it worked fine. I just wonder whether I should not send primitive type values.
e

e5l

07/02/2019, 7:46 AM
Hi @lenqnr, I can't reproduce it with simple echo server and installed client Json feature. Could you provide the server handler?
l

lenqnr

07/02/2019, 1:50 PM
@e5l Thanks for the reply. It was my mistake. Here's a reproducible client code.
Copy code
fun main() = runBlocking {
    val httpClient = HttpClient(CIO) {
        install(JsonFeature) {
            serializer = GsonSerializer()
        }
    }

    <http://httpClient.post|httpClient.post><Unit> {
        url("<http://localhost:8080>")
        contentType(ContentType.Application.Json)
        body = "hello"
    }
}
I made a function for the
post
with a parameter for the
body
values and sent a String value with "Content-Type: application/json". I gotta add a parameter
contentType
or make another function for primitive types.
5 Views