https://kotlinlang.org logo
#ktor
Title
# ktor
a

Ankit Dubey

06/03/2021, 10:13 AM
Hi, I’m stuck in adding interceptor and CertificatePinner in Default Engine of Ktor i.e.
HttpClient()
I came to know, that I can do it using HttpClient(OkHttp) engine, but in KMM project, I’m not allowed to add OkHttp dependency in shared/commonMain module. It’s saying
Copy code
Failed building KotlinMPPGradleModel
org.gradle.internal.resolve.ArtifactNotFoundException: Could not find ktor-client-okhttp-1.6.0-samplessources.jar (io.ktor:ktor-client-okhttp:1.6.0).
//Something I’ve achieve yet.
Copy code
var cert : CertificatePinner = CertificatePinner.Builder()
    .add(baseURL, "***************")
    .build()

val ktorHttpClient = HttpClient(OkHttp) {
    engine {
        config {
            certificatePinner(cert)
        }
    }
What I need
Copy code
val ktorHttpClient = HttpClient() {

    engine {
        //ADD CERTIFICATE PINNER
        //ADD INTERCEPTOR
    }
e

El Zhang

06/04/2021, 1:26 AM
Please add OkHttp client to shared/androidMain, and make an expect/actual class/function to export your Engine to the commonMain.
✔️ 1
a

Ankit Dubey

06/04/2021, 4:17 AM
Good idea. Hope It’ll work for me. Thanks a lot.
It worked 😄 I did something like this
Copy code
expect class KtorClientProvider() {
    val ktorClient: HttpClient
}
Thanks again @El Zhang
👍 1
7 Views