Hey team! need your help! i've been working on a C...
# ktor
e
Hey team! need your help! i've been working on a Compose Multiplatform App. I added Ktor to fetch data from an API, for testing the API i used Postman... The API is not very complex. Just add an api-key to the headers. In postman the response is almost immediate. On the apps side, sometimes it takes a while and many times it even returns this error message: Socket timeout has expired [url=******, socket_timeout= unknown]ms... Do you have any idea what it could be? 🫶
this is my ktor client method:
Copy code
@Composable fun KtorClientConfig(): HttpClient { val client = remember { HttpClient { install(ContentNegotiation) { json(json = Json { ignoreUnknownKeys = true }, contentType = ContentType.Any) } } } return client
 }
h
Which platform/engine do you use?
e
sorry, what do you mean with that? im working on a Compose Multiplatform project: adding all the logic and ui in the common main
h
Yes, but you do use a particular Ktor engine (eg Android/JS/Darwin) that actually executes the request. Or do you really can reproduce the error on all engines?
e
oh i kown, yes i can reproduce it on Android and iOS! the two platform i support
a
Do you execute your application from within an emulator or a physical device? Can you share the URL you make the requests to if it's a public one?
e
both in a real device...sorry its a private URL with personal information
and a private api-key
one suggestion i found was to add timeout:
Copy code
@Composable
fun KtorClientConfig(): HttpClient {
    val client = remember {
        HttpClient() {
            install(ContentNegotiation) {
                json(json = Json { ignoreUnknownKeys = true }, contentType = ContentType.Any)
            }
            
            install(HttpTimeout) {
                requestTimeoutMillis = 30000 
                connectTimeoutMillis = 20000 
                socketTimeoutMillis = 20000 /
            }
        }
    }
    return client
}
a
I suggest making the request with another HTTP client from the device to check whether the problem is in the Ktor client.
e
what do you mean?
a
Instead of checking the connection via Postman from the local host, you could check it by making the request from the real device with another HTTP client. This way, you can determine if it's a general connection problem or a problem with the Ktor client.
e
Like using Retrofit? yea i can check that
a
Yes