Ulrich Winter
06/26/2023, 1:19 PMResponseBodyReadyForSend hook?
I have found the onCallRespond callback, but I want to be sure, that I get the final response body content which is not further transformed or modified before sent to the client.Aleksei Tirman [JB]
06/26/2023, 1:39 PMon(ResponseBodyReadyForSend) is the response body after all transformations.Aleksei Tirman [JB]
06/26/2023, 1:39 PMval plugin = createApplicationPlugin("myplugin") {
on(ResponseBodyReadyForSend) { call, content ->
println(content)
}
}Ulrich Winter
06/26/2023, 1:44 PMOutgoingContent which has several properties like contentType, headers, status but none for the response body itself.
How doe I get the response body from that content?Aleksei Tirman [JB]
06/26/2023, 1:51 PMOutgoingContent is a sealed class, you can check the actual type and, depending on it, read the body. Here is an example:
on(ResponseBodyReadyForSend) { call, content ->
when(content) {
is OutgoingContent.ByteArrayContent -> {
content.bytes() // ByteArray
}
is OutgoingContent.ReadChannelContent -> {
content.readFrom() // ByteReadChannel
}
// ...
}
}Ulrich Winter
06/26/2023, 1:52 PMUlrich Winter
06/26/2023, 1:58 PMResponseBodyReadyForSend is not possible, because:
• adding it to ``call.response.headers` has no effect
• modifying content.headers is not possible, as io.ktor.http.Headers has no methods for appending an header
Right?Aleksei Tirman [JB]
06/27/2023, 8:09 AMcall.response.headers should work.Ulrich Winter
06/27/2023, 8:12 AM