Open Tetraquark opened 3 years ago
@Tetraquark hi! i think your case can be solved by something like Logger ktor feature. in this feature all content received (for logs) and later it passed to next pipeline step (just as copy as i know)
code that help:
scope.receivePipeline.intercept(HttpReceivePipeline.After) { response ->
val charset = response.contentType()?.charset() ?: Charsets.UTF_8
// Get body from content
val body = response.content.readRemaining().readText(
charset = charset
)
// Processing body
val result = handleBody(body)
if (result) {
// Just proceed pipeline
val byteChannel = ByteReadChannel(
body.toByteArray(charset)
)
proceedWith(context.wrapWithContent(byteChannel).response)
} else {
throw Exception()
}
}
There is a situation when optional additional handling of a content of a server response in generated API classes by moko-network-generator plugin is needed. I need to pass some lambda or
fun interface
into constructor of*Impl
classes. Then the lambda will be called after results receiving and before serialization process (here before 131 line): https://github.com/icerockdev/moko-network/blob/1b6bf98cb58752e1bc6f6df1d330826f8c3ef843/network-generator/src/main/resources/kotlin-ktor-client/api.mustache#L131Use case: HTTP-server responds with errors using HTTP status 200 and there is some data of the error in the request body. As I understand it, at the moment there is no way to handle such thing using Ktor features because of
io.ktor.client.call.DoubleReceiveException
.