Hi there, how can I pass a cookie at Ktor client h...
# ktor
p
Hi there, how can I pass a cookie at Ktor client headers? I am trying the below code
Copy code
header("Cookie", cookie)
but Ktor seems to ignore it.
a
There is the
cookie
function to send Cookies. You can find an example in the documentation.
h
Use the
cookie()
function
p
@Aleksei Tirman [JB] the link is a 404
a
Oops sorry, fixed it.
👍 1
p
I am using cookie() like this with no result:
Copy code
client.get("myEndpoint") {
            cookie(name = "JSESSIONID", value = "uLOgaxWu7uF4tic5sBrXkBzvxB-rDWScF-fTDvB6Mj4F243gvhdo!-128011973")
}
I am doing something wrong?
a
Seems correct. The following code works as expected:
Copy code
val client = HttpClient(CIO)

val r = client.get<String>("<https://httpbin.org/get>") {
    cookie(name = "JSESSIONID", value = "uLOgaxWu7uF4tic5sBrXkBzvxB-rDWScF-fTDvB6Mj4F243gvhdo!-128011973")
}

println(r)
How do you check the result?
p
With 2 ways: 1. Checking the logs 2. Getting an error from the API call
btw I am testing it from Android client
a
The same code I've posted works on the Android emulator with the
Android
engine. Could you please check the actual presence of a
Cookie
header in the Wireshark?
p
Wireshark shows the same logs
Very strange
@Aleksei Tirman [JB] could you plz show me your logs in order to see how it is appearing?
a
Copy code
GET / HTTP/1.1
Cookie: JSESSIONID=uLOgaxWu7uF4tic5sBrXkBzvxB%2DrDWScF%2DfTDvB6Mj4F243gvhdo%21%2D128011973
Accept-Charset: UTF-8
Accept: */*
User-Agent: Ktor client
Content-Length: 0
Host: localhost:7070
Connection: Keep-Alive
Accept-Encoding: gzip

HTTP/1.1 404 Not Found
Content-Length: 0
Connection: keep-alive
👍 1
I added
android:usesCleartextTraffic="true"
to make an insecure connection.
👍 1
p
I managed to send it when I passed it like this:
Copy code
defaultRequest {
                cookie(
                    "JSESSIONID",
                    "uLOgaxWu7uF4tic5sBrXkBzvxB-rDWScF-fTDvB6Mj4F243gvhdo!-128011973"
                )
            }
hm it appears like it has urlEnconding
Copy code
JSESSIONID=uLOgaxWu7uF4tic5sBrXkBzvxB%2DrDWScF%2DfTDvB6Mj4F243gvhdo%21%2D128011973
@Aleksei Tirman [JB] I have a clear status of the issue I am dealing with now. If I set:
Copy code
install(HttpCookies) {
                storage = AcceptAllCookiesStorage()
            }
then all my “custom” cookies are being ignored. If I remove that then everything plays well. The thing is that I need to initially read the cookie from an API call and the AcceptAllCookiesStorage() declaration seems to me that is necessary.
a
@ptsiogas seems like this behavior is wrong. Cookies are removed because of this line. I've created an issue KTOR-3105 to address this problem.
👍 1
978 Views