I’m trying to inject a header into a repsonse, thi...
# ktor
c
I’m trying to inject a header into a repsonse, this seems to be awfully complicated….does anyone have an example on how to do this? Basically, on a low level I grab the remote host IP and want to append a custom header to the response so higher levels of the network stack know where the response came from. On Android, it’s easy with a OkHttp interceptor. But on iOS using NSURLSession, I’m unable to inject the header. Trying to use
intercept
of
HttpSend
a
You can use this workaround to modify the response from a server.
c
1000 thanks! That looks simple enough. I'll give it a try
In case sombody finds this….there is an even easier solution, I didn’t need to set a header at all. It’s much simpler to use the call attributes.
Copy code
install("OctoRemoteIp") {
    receivePipeline.intercept(HttpReceivePipeline.State) { response ->
        withTimeoutOrNull(1.seconds) {
            val host = response.request.url.host
            dataDelegate.remoteAddressCache.first { it.containsKey(host) }[host]
        }?.let { remoteIp ->
             response.call.attributes.put(OctoRemoteIpAttributeKey, remoteIp)
        }
        proceedWith(response)
    }
}