https://kotlinlang.org logo
Title
p

ptsiogas

09/01/2021, 10:01 AM
Hi there, how can I pass a cookie at Ktor client headers? I am trying the below code
header("Cookie", cookie)
but Ktor seems to ignore it.
a

Aleksei Tirman [JB]

09/01/2021, 10:04 AM
There is the
cookie
function to send Cookies. You can find an example in the documentation.
h

hfhbd

09/01/2021, 10:04 AM
Use the
cookie()
function
p

ptsiogas

09/01/2021, 10:04 AM
@Aleksei Tirman [JB] the link is a 404
a

Aleksei Tirman [JB]

09/01/2021, 10:05 AM
Oops sorry, fixed it.
👍 1
p

ptsiogas

09/01/2021, 10:15 AM
I am using cookie() like this with no result:
client.get("myEndpoint") {
            cookie(name = "JSESSIONID", value = "uLOgaxWu7uF4tic5sBrXkBzvxB-rDWScF-fTDvB6Mj4F243gvhdo!-128011973")
}
I am doing something wrong?
a

Aleksei Tirman [JB]

09/01/2021, 10:22 AM
Seems correct. The following code works as expected:
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

ptsiogas

09/01/2021, 10:30 AM
With 2 ways: 1. Checking the logs 2. Getting an error from the API call
btw I am testing it from Android client
a

Aleksei Tirman [JB]

09/01/2021, 10:44 AM
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

ptsiogas

09/01/2021, 11:05 AM
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

Aleksei Tirman [JB]

09/01/2021, 11:25 AM
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

ptsiogas

09/01/2021, 11:33 AM
I managed to send it when I passed it like this:
defaultRequest {
                cookie(
                    "JSESSIONID",
                    "uLOgaxWu7uF4tic5sBrXkBzvxB-rDWScF-fTDvB6Mj4F243gvhdo!-128011973"
                )
            }
hm it appears like it has urlEnconding
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:
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

Aleksei Tirman [JB]

09/01/2021, 6:38 PM
@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