https://kotlinlang.org logo
Title
d

darkmoon_uk

06/16/2019, 1:16 AM
Is it always necessary to install the
ContentNegotiation
feature in the server to receive & send serialized (JSON encoded) objects? If the expected, exact content type is specified on both server and receiver, I don't see why
ContentNegotiation
needs to be a hard requirement - I thought this was only about dynamically negotiating a variable content & type at run-time?
d

Dico

06/16/2019, 2:01 AM
It's not necessary. You can do this for example without installing the feature:
get("/") {
                call.respond(TextContent("{\"key\": \"value\"}", contentType = ContentType.Application.Json))
            }
And you can test request content type:
if (call.request.contentType() == ContentType.Application.Json) {
                    val text = call.receiveText()
                    // deserialize text
                }
The content negotiation feature is also used to install serialization mechanisms, so you can't to call.receive<TYPE>() without it