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

Gary

03/22/2019, 2:51 AM
Hi there, sorry new to ktor - wondering if someone could point me in the direction of what is the best practice to validating that a header is present in a request. E.g. something equivalent to Spring
Copy code
@RequestHeader(value = "X-API-Key", required = true)
👆 1
s

spand

03/22/2019, 7:22 AM
Pretty sure there is a
header
function in the routing setup
d

dave08

03/24/2019, 5:27 AM
Maybe a little custom Feature that hooks the requests and returns whatever if the header's not there...
g

Gary

03/25/2019, 12:08 AM
We ended up writing something like this. Feels a bit hacky though 😕
Copy code
fun Route.requireHeaders(vararg headerNames: String, build: Route.() -> Unit): Route {
    val selector = HttpHeaderExistRouteSelector(headerNames)
    return createChild(selector).apply(build)
}
And then using it in the routes
Copy code
route("endpoint") {
            requireHeaders("REQUIRED_HEADER", "REQUIRED_HEADER_2") {
                get("available") {
...                    
                }
            }
        }
d

dave08

03/25/2019, 11:10 AM
Not a bad solution 😉. How would you have preferred it to look?
3 Views