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
Aleksei Tirman [JB]
11/03/2022, 9:52 AM
What do you mean by useful for yourself?
a
Austin Pederson
11/07/2022, 11:20 PM
Like the JSON content for example
a
Aleksei Tirman [JB]
11/08/2022, 11:38 AM
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)
}