I’m running into issue when I’m rewriting response...
# ktor
g
I’m running into issue when I’m rewriting response headers like this:
Copy code
install("Rewrite-bad-response-headers") {
   receivePipeline.intercept(HttpReceivePipeline.State) { response ->
     proceedWith(
         object : HttpResponse() {
             override val call: HttpClientCall = response.call
             override val status: HttpStatusCode get() = response.status
             override val version: HttpProtocolVersion = response.version
             override val requestTime: GMTDate = response.requestTime
             override val responseTime: GMTDate = response.responseTime
             override val content: ByteReadChannel = response.content
             override val headers = response.headers.fixed()
             override val coroutineContext: CoroutineContext = response.coroutineContext
          }
     )
   }
}
it works fine until I install logger (order doesn’t matter):
Copy code
install(Logging) {
   logger = object : Logger {
      override fun log(message: String) {
          SomeLogger.v(message)
      }
   }
   level = LogLevel.HEADERS
}
then my headers are gone and replaced with original request. Any ideas what might be wrong?
a
Do you mean the headers logged by the
Logging
plugin or returned by the
HttpResponse.headers
method?
g
nope, (but I noticed that it is printing original response 😅). I actually debugged and this is what I get after call execute in Auth plugin
Copy code
scope.plugin(HttpSend).intercept { context ->
                val origin = execute(context)
                if (origin.response.status != HttpStatusCode.Unauthorized) return@intercept origin
                if (origin.request.attributes.contains(AuthCircuitBreaker)) return@intercept origin
this works ok for LogLevels.HEADERS and INFO, but breaks when BODY or ALL is set
a
That's because the response is replaced in the later
HttpReceivePipeline.After
phase. To solve the problem, add and intercept the phase after
HttpReceivePipeline.After
phase:
Copy code
val client = HttpClient(OkHttp) {
    install(Logging) {
        level = LogLevel.BODY
    }
}

val phase = PipelinePhase("after after")
client.receivePipeline.insertPhaseAfter(HttpReceivePipeline.After, phase)
client.receivePipeline.intercept(phase) { response ->
    proceedWith(
        object : HttpResponse() {
            override val call: HttpClientCall = response.call
            override val status: HttpStatusCode get() = response.status
            override val version: HttpProtocolVersion = response.version
            override val requestTime: GMTDate = response.requestTime
            override val responseTime: GMTDate = response.responseTime
            override val content: ByteReadChannel = response.content
            override val headers = headersOf("custom" to listOf("123"))
            override val coroutineContext: CoroutineContext = response.coroutineContext
        }
    )
}
1