Search code examples
kotlinktorkotlin-multiplatform-mobile

Unresolved reference: responsePipeline in ktor


Hey I am working in ktor. I am following this answer. I want to use responsePipeline on my androidMain. But I am getting error Unresolved reference: responsePipeline. Actually I created my HttpClient with OkHttp in my androidMain. I don't know why I cannot use in my file. Can someone please guide me.

androidMain

actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(OkHttp) {
    responsePipeline.intercept(HttpResponsePipeline.Transform) {
    }
    config(this)
    install(Logging) {
        logger = Logger.SIMPLE
        level = LogLevel.BODY
    }
}

commonMain

expect fun httpClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient

Error

enter image description here


Solution

  • responsePipeline is a property of HttpClient class, and you are trying to access it on an instance of HttpClientConfig.

    Lamda of HttpClient(OkHttp) returns an object of type HttpClientConfig which doesn't have a property named reponsePipeline.

    To use responsePipeline you have to create an instance of HttpClient, and after that you can use it.

    You can create a method which returns your configured httpClient

    fun createHttpClient(config: HttpClientConfig<*>.() -> Unit): HttpClient {
        val httpClient = HttpClient(OkHttp) {
            config(this)
            install(Logging) {
                logger = Logger.SIMPLE
                level = LogLevel.BODY
            }
        }
        httpClient.responsePipeline.intercept(HttpResponsePipeline.Transform) {
    
        }
        return httpClient
    }
    

    Assign this to the actual definition.

    actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = createHttpClient(config)