Johnny Baloney
02/07/2022, 4:22 PMKtor now supports a way to construct a JSON body using theDSL:kotlinx.serialization
<http://client.post|client.post>("<http://localhost:9090>") {
contentType(ContentType.Application.Json)
body = json {
"key1" to 123
"map" to json {
"key2" to "abc"
}
}
}
To use it on a client, install JsonFeature and add the ktor-client-serialization dependency.When I paste this into the editor
json
function is not recognised. How do I enable the kotlinx.serialization DSL?Angel Mitev
02/07/2022, 7:55 PMAleksei Tirman [JB]
02/08/2022, 7:44 AMval client = HttpClient(Apache) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
}
<http://client.post|client.post>("<http://localhost:9090>") {
contentType(ContentType.Application.Json)
body = buildJsonObject {
put("key1", 123)
putJsonObject("map") {
put("key2", "abc")
}
}
}
Johnny Baloney
02/08/2022, 10:23 AMbody = JsonObject(
mapOf(
"id" to JsonPrimitive(3),
"title" to JsonPrimitive("some title"),
"author" to JsonPrimitive("some author")
)