Hi there, sorry new to ktor - wondering if someone...
# ktor
g
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
Pretty sure there is a
header
function in the routing setup
d
Maybe a little custom Feature that hooks the requests and returns whatever if the header's not there...
g
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
Not a bad solution 😉. How would you have preferred it to look?