the body gets consumed. I have a plugin where I want to read the body to respond to certain types or errors, but the response should still get sent out of this plugin. So I am having a problem being able to see a response body both inside and outside of a plugin
My problem does sound exactly like what the commenter grasseh in there mentioned two months ago. I want to see the response body in my ClientPlugin but if I do, the final response fails to deserialize (because the response is gone)
a
Aleksei Tirman [JB]
06/26/2024, 7:16 PM
You can split the body's
ByteReadChannel
into two channels to use one of them and return another. Here is an example:
Copy code
val client = HttpClient(CIO)
val scope = CoroutineScope(coroutineContext)
client.responsePipeline.intercept(HttpResponsePipeline.Receive) { (type, body) ->
if (body !is ByteReadChannel) return@intercept
val (first, second) = body.split(scope)
println(first.toByteArray().size) // Consume first here
proceedWith(HttpResponseContainer(type, second))
}
client.prepareGet("<https://httpbin.org/get>").execute { // Stream the response body
println(it.bodyAsText())
}