Hello, I am using Ktor HttpClient with CookieStora...
# android
v
Hello, I am using Ktor HttpClient with CookieStorage (CookieStore) to send http requests to a Rest API server. I have noticed that overtime, especially if the app is made to run in the background by locking the phone screen or leaving it idle for a while, the CookieStorage appears to decay away and the subsequent requests no longer get authenticated. This leads me to believe that the requests no longer automatically attach the cookie from the CookieStore. I surmise it is because the CookieStorage probably gets killed away by the Android system as the app goes to the background? Could this be the case? Or maybe there's something else, maybe I am mismanaging lifecycles or something? But I am not sure if that is possible, given that my Ktor HttpClient methods are placed in a singleton:
Copy code
object HttpUtils {
    val aastorage = AcceptAllCookiesStorage()
    val client = HttpClient(CIO) {
        expectSuccess = true
        install(ContentNegotiation) {
            json(Json {
                prettyPrint = true
                isLenient = true
                ignoreUnknownKeys = true
            })
        }

        install(HttpCookies) {
            storage = aastorage
        }

        install(Logging) {
            logger = Logger.SIMPLE
        }
    }

    private val config = ConfigLoader.getConfig()

    suspend fun installCookies(cookies: List<Cookie>) {
        cookies.forEach {
            aastorage.addCookie(
                Url(config.base_url), Cookie(
                    name = it.name,
                    value = it.value,
                    maxAge = it.maxAge,
                    expires = it.expires,
                    domain = it.domain,
                    path = it.path,
                    secure = it.secure,
                    httpOnly = it.httpOnly,
                    extensions = it.extensions,
                    encoding = it.encoding
                )
            )
        }
    }

    suspend inline fun get(request: HttpGetRequest): Result<HttpResponse> {
        return withContext(Dispatchers.IO) {

            try {
                val response: HttpResponse = client.get(request.url)

                Result.success(response)
            } catch (e: Exception) {
                Result.failure(e)
            }
        }
    }

    suspend inline fun <reified P> post(request: HttpPostRequest<P>): Result<HttpResponse> {
        return withContext(Dispatchers.IO) {
            try {
                val response: HttpResponse = client.post(request.url){
                    contentType(ContentType.Application.Json)
                    setBody(request.payload)
                }
                Result.success(response)
            } catch (e: Exception) {
                Result.failure(e)
            }
        }
    }

    suspend inline fun <reified P> patch(request: HttpPatchRequest<P>): Result<HttpResponse> {
        return withContext(Dispatchers.IO) {
            try {
                val response: HttpResponse = client.patch(request.url){
                    contentType(ContentType.Application.Json)
                    setBody(request.payload)
                }
                Result.success(response)
            } catch (e: Exception) {
                Result.failure(e)
            }
        }
    }


}
This singleton is then used by my repository classes. Could you kindly suggest any recommended strategies to pinpoint and address this issue? Thank you
c
I would ask in #ktor but I suspect
AcceptAllHeadersStorage
is just In-Memory and if the app is in background, it might get killed and you loose the cookies.
v
@Chrimaeon ah, my bad, for some reason I did not notice the channel. Should I move my message there or should I copy it?
c
I’d copy a link to it. Your question somehow related to Android and then there is still the reference.
v
Will do, thank you. By the way, regarding your proposal, maybe I could reinstatiate the AcceptAllHeadersStorage with onResume?
c
People in the #ktor channel will know a solution. There are also Android folks which might have had the same issue.