I'd like to create a cookie when the client makes ...
# ktor
c
I'd like to create a cookie when the client makes a specific request. If I understand correctly, that means I need to add a
Set-Cookie
header in the response. However,
call.respond
doesn't take a lambda to configure the request, unlike its client-side equivalent. I found
call.respondText
, which has an override with a lambda with an
OutgoingContent
receiver, which has a
headers
attribute, however it seems to be immutable. How can I add a specific header for a specific request? I don't want that header to be present in other requests.
a
You can use
ResponseCookies
object to append cookies for a response:
Copy code
routing {
    get("/") {
        call.response.cookies.append("name1", "value")
        call.response.cookies.append("name2", "value")
        call.respond(HttpStatusCode.OK)
    }
}
c
Oh, that's nice! And it also answers the next question I had because that also works the other way around with
call.request
.
Thanks a lot, it's a shame that it's never mentioned in the docs about cookies (they only explain sessions)
d
hi, you can find how to specify various response parameters (including cookies) in the
Set response parameters
section: https://ktor.io/docs/responses.html#cookies
c
Ah, indeed. It doesn't appear in the table of contents nor in Google results so I couldn't find it.