Is there any way to get the response body from a p...
# ktor
a
Is there any way to get the response body from a plugin? I know I can get the OutgoingContet, but I can't figure out how to turn that into something usable for myself
a
What do you mean by useful for yourself?
a
Like the JSON content for example
a
You can use the transformBody handler to observe or transform response body before sending it to a client:
Copy code
val plugin = createApplicationPlugin("plugin") {
    onCallRespond { call ->
        transformBody { data ->
            if (data is Data) {
                Data(data.x + 1)
            } else {
                data
            }
        }
    }
}

@Serializable
data class Data(val x: Int)

fun main() {
    embeddedServer(Netty, port = 8085) {
        install(plugin)
        install(ContentNegotiation) {
            json()
        }
        routing {
            get("/json") {
                call.respond(Data(123))
            }
        }
    }.start(wait = true)
}
🙏 1