# Why is my cookie not persisting?

**URL:** <https://discuss.kotlinlang.org/t/why-is-my-cookie-not-persisting/28950>\
**Category:** Support\
**Created:** [July 23, 2024, 12:25am UTC](https://discuss.kotlinlang.org/t/why-is-my-cookie-not-persisting/28950 "2024-07-23T00:25:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cozy-Benchmark](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@Cozy-Benchmark](https://discuss.kotlinlang.org/u/Cozy-Benchmark)\
**Post date:** [July 23, 2024, 12:25am UTC](https://discuss.kotlinlang.org/t/why-is-my-cookie-not-persisting/28950/1 "2024-07-23T00:25:02Z")

</div>

I’m facing an issue regarding my cookies. I have implemented a frontend logic using okhttp3 and retrofit to connect to my backend. I am able to log in my users and have a response containing my cookie but once I send other request, my cookies are not part of the request and seems to be gone.

SessionCookieJar.kt

```auto
object SessionCookieJar {
    private val cookieStore: MutableMap<String, List<Cookie>> = mutableMapOf()

    private val cookieJar = object : CookieJar {
        override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
            println("Saving Cookies from response for URL: $url")
            cookieStore[url.host] = cookies
            println("Cookies: $cookies")
        }

        override fun loadForRequest(url: HttpUrl): List<Cookie> {
            val cookies = cookieStore[url.host] ?: emptyList()
            println("Loading Cookies for request to URL: $url")
            println("Cookies: $cookies")

            return cookies
        }
    }

    val okHttpClient: OkHttpClient = OkHttpClient.Builder()
        .cookieJar(cookieJar)
        .build()

    private val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl("https://7a11-222-127-105-220.ngrok-free.app") // replace with ngrok forwarding port
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val apiService: ApiService = retrofit.create(ApiService::class.java)

    fun printCookies() {
        println("Stored Cookies:")
        cookieStore.forEach { (host, cookies) ->
            println("Host: $host")
            cookies.forEach { cookie ->
                println("Cookie: ${cookie.name}=${cookie.value}")
            }
        }
    }
}

```

Service - fetch request

```auto
  suspend fun fetchInventory(): List<Inventory> {
        return try {
            println("Fetching Inventory...")
            val response = SessionCookieJar.apiService.fetchInventory(
                filterStock, filterStatus, filterCategory, filterTags, sortType, sortOrder
            )
            SessionCookieJar.printCookies()
            if (response.isSuccessful) {
                response.body() ?: emptyList()
            } else {
                println("Response unsuccessful: ${response.code()}")
                emptyList()
            }
        } catch (e: Exception) {
            println("An Error Occurred (Fetch Inventory): $e")
            emptyList()
        }
    }

```

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [July 26, 2024, 5:15am UTC](https://discuss.kotlinlang.org/t/why-is-my-cookie-not-persisting/28950/2 "2024-07-26T05:15:12Z")

</div>

First obvious question: what is your log output? Rather than logging the whole `HttpUrl` object, can you log just the `url.host`, since that’s the key for storing/retrieving Cookies? When `loadForRequest` is being called, is it returning the list of Cookies you expect?
