kotlin - Setting Cookies before redirect with Ktor - Stack Overflow

I have the following code:import io.ktor.serialization.kotlinx.json.*import io.ktor.server.applicatio

I have the following code:

import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.serverty.*
import io.ktor.server.plugins.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun main() {
    embeddedServer(Netty, 8087) {

        fun isSecureRequest(call: ApplicationCall): Boolean = call.request.origin.scheme == "https"
        install(ContentNegotiation) {
            json()
        }
        routing {
            get("/cookies") {
                val cookies = call.request.cookies.rawCookies
                call.respond(mapOf("cookies" to cookies))
            }

            get("/cookies/set/{name}/{value}") {
                val name = call.parameters["name"] ?: ""
                val value = call.parameters["value"] ?: ""
                val secure = isSecureRequest(call)

                //call.response.cookies.append(Cookie(name, value, secure = secure))
                call.response.cookies.append(name, value, secure = secure)
                call.respondRedirect("/cookies")
            }

            get("/cookies/set") {
                val secure = isSecureRequest(call)
                call.request.queryParameters.entries().forEach { (key, values) ->
                    values.forEach { value ->
                        call.response.cookies.append(key, value, secure = secure)
                    }
                }
                call.respondRedirect("/cookies")
            }
        }
    }.start(wait = true)
}

When following the url: http://localhost:8087/cookies/set?cookieSetFromQuery=hello the cookie is set and I see it in the response.

However, if use the variant with the path parameters: http://localhost:8087/cookies/set/cookieFromPath/hello i do not see the cookie in the response.

Even after going through the debugger I do not understand why is there a difference in behavior.

In both cases, the cookies are set with all.response.cookies.append(name, value, secure = secure) and in both cases a redirect to the /cookies path is made. Why is the variant using the path parameters not working?

I have the following code:

import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.serverty.*
import io.ktor.server.plugins.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun main() {
    embeddedServer(Netty, 8087) {

        fun isSecureRequest(call: ApplicationCall): Boolean = call.request.origin.scheme == "https"
        install(ContentNegotiation) {
            json()
        }
        routing {
            get("/cookies") {
                val cookies = call.request.cookies.rawCookies
                call.respond(mapOf("cookies" to cookies))
            }

            get("/cookies/set/{name}/{value}") {
                val name = call.parameters["name"] ?: ""
                val value = call.parameters["value"] ?: ""
                val secure = isSecureRequest(call)

                //call.response.cookies.append(Cookie(name, value, secure = secure))
                call.response.cookies.append(name, value, secure = secure)
                call.respondRedirect("/cookies")
            }

            get("/cookies/set") {
                val secure = isSecureRequest(call)
                call.request.queryParameters.entries().forEach { (key, values) ->
                    values.forEach { value ->
                        call.response.cookies.append(key, value, secure = secure)
                    }
                }
                call.respondRedirect("/cookies")
            }
        }
    }.start(wait = true)
}

When following the url: http://localhost:8087/cookies/set?cookieSetFromQuery=hello the cookie is set and I see it in the response.

However, if use the variant with the path parameters: http://localhost:8087/cookies/set/cookieFromPath/hello i do not see the cookie in the response.

Even after going through the debugger I do not understand why is there a difference in behavior.

In both cases, the cookies are set with all.response.cookies.append(name, value, secure = secure) and in both cases a redirect to the /cookies path is made. Why is the variant using the path parameters not working?

Share asked Mar 7 at 14:04 zelitezelite 1,50317 silver badges40 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The reason must be that the cookie set from the /cookies/set path unlike /cookies/set/cookieFromPath/hello, is within the scope of the /cookies path and therefore browser sends the Cookie header.

To solve the problem, you can specify the explicit path for the Set-Cookie header:

call.response.cookies.append(name, value, path = "/cookies", secure = secure)

You can find more information about the Path attribute on MDN.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744925185a4601388.html

相关推荐

  • kotlin - Setting Cookies before redirect with Ktor - Stack Overflow

    I have the following code:import io.ktor.serialization.kotlinx.json.*import io.ktor.server.applicatio

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信