Hey guys! I’m currently developing an API using Ktor and I need to inspect all incoming call for a specific header and return a HTTP 400 error if it’s not present. My understanding is that I could use an intercept for that purpose but I’m a bit confused where I should define and use it.
This is how my project is setup:
Copy code
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module() {
registerRoutes()
}
fun Application.registerRoutes() {
routing {
route("/") {
get {
call.respondText("Hello world!")
}
}
}
}
What would be the best way? Where should I put it?
Thanks!
a
Aleksei Tirman [JB]
03/30/2022, 8:51 AM
You can add an interceptor in the definition of the
Application.module
function. Here is an example:
Copy code
fun Application.module() {
intercept(ApplicationCallPipeline.Setup) {
if (!call.request.headers.contains("my-header")) {
call.respond(HttpStatusCode.BadRequest)
}
}
// ...
}