https://kotlinlang.org logo
#ktor
Title
# ktor
y

Yyq2008

01/30/2020, 9:29 AM
Copy code
post("/oauth/token") {
    val gt = call.parameters.get("grant_type") ?: call.respond(HttpStatusCode.BadRequest, mapOf("error" to "no grant_type specified"));
    call.respondText(call.request.headers["Authorization"] ?: "No header")
}
how to response and return to avoid run the second response?
s

spand

01/30/2020, 10:44 AM
Something like this?
Copy code
post("/oauth/token") {
param("grant_type") {
handle {
    val gt = call.parameters.get("grant_type")!! call.respond(HttpStatusCode.BadRequest, mapOf("error" to "no grant_type specified"));
    call.respondText(call.request.headers["Authorization"] ?: "No header")
}
}
}
y

Yyq2008

01/30/2020, 12:27 PM
No, I want just want to finish the response if no grant type found.
Thanks, I found the solution:
Copy code
post("/oauth/token") {
    val gt = call.parameters.get("grant_type") ?: run {
        call.respond(
            HttpStatusCode.BadRequest,
            mapOf("error" to "no grant_type specified")
        );
        return@post
    }
    call.respondText(call.request.headers["Authorization"] ?: "No header")
}
d

Dico

01/31/2020, 10:52 AM
Why dont you just do
Copy code
if (gt == null) {...}
?
y

Yyq2008

02/15/2020, 5:26 AM
to avoid if
11 Views