I’m trying out KMM using Ktor as client, with OkHt...
# ktor
j
I’m trying out KMM using Ktor as client, with OkHttp engine for Android and Darwin for iOS. I am making a request like below
Copy code
suspend fun getUpcomingLaunches(): List<Launch> =
    client.get {
        url {
            protocol = URLProtocol.HTTPS
            host = "<http://api.spacexdata.com/v4|api.spacexdata.com/v4>"
            encodedPath = "launches/upcoming"
        }
    }.body()
This works fine on Android, however on iOS it is failing with 404 not found. From the logs it looks like the correct url is being built (https://api.spacexdata.com/v4/launches/upcoming) although obviously it is not calling that url exactly. However, f I change the code though to
Copy code
suspend fun getUpcomingLaunches(): List<Launch> =
    client.get("<https://api.spacexdata.com/v4/launches/upcoming>").body()
it works as expected. I’m guessing it is a bug in Darwin engine, but just wanted to check I’m not doing something stupid or if it’s a known issue. TIA!
Ok, I think I’ve figured out what the issue is. It seems that Darwin is more strict with the ‘host’ parameter. So updating to
Copy code
suspend fun getUpcomingLaunches(): List<Launch> =
    client.get {
        url {
            protocol = URLProtocol.HTTPS
            host = "<http://api.spacexdata.com|api.spacexdata.com>"
            encodedPath = "v4/launches/upcoming"
        }
    }.body()
(ie. moving
v4
from
host
to
encodePath
) fixes the issue. Strange that the behaviour is different for the different engines but 🤷‍♂️
Made more confusing by the fact that the Logging plugin behaves the same way as OkHttp client, so for iOS it was showing request to https://api.spacexdata.com/v4/launches/upcoming while actually calling https://api.spacexdata.com/launches/upcoming
a
Could you please file an issue about this problem?
j
Yep, will do