Hello, When I try to make a request on a route th...
# ktor
j
Hello, When I try to make a request on a route that does not exist, instead of receiving a 404 I systematically receive a 200 code. Even if I use the StatusPages plugin and I try to modify the behavior of the NotFound status as indicated in the documentation, nothing happens. The 404 code and status are never sent. Do any of you know where this can come from? I confess that I do not see at all where it can come from and I can not isolate the code responsible for this so difficult to send you an example. I specify that the trace marks well that the road is not known:
Copy code
/, segment:0 -> SUCCESS @ /
    /(method:GET), segment:0 -> FAILURE "Not all segments matched" @ /(method:GET)
    /error, segment:0 -> FAILURE "Selector didn't match" @ /error
Matched routes:
  No results
Route resolve result:
  FAILURE "No matched subtrees found" @ /
a
It’s difficult to say only by seeing a routing trace. Some plugin in the middle may respond with the 200 status code.
j
If it helps, here is the list of plugins I use: • Koin • Cors • DefaultHeaders • Compression • ContentNegotiation • CallLogging • StatusPages • Authentication • Routing I disabled some of them, one by one, to try to isolate the problem but without success.
I thought it was because of StatusPages but no, once deactivated I still get 200 codes
a
Are you able to share the code of your project?
Also, please describe a request that you’re making.
j
It depends on the part of the project, I try to make a issue sample project
The request is simple, get without parameters on localhost:8080/something and without auth
localhost:8080/something should return 404
If I simply add the route and ask to respond with a 404 it works. But if I remove the code I get a 200.
Copy code
get("/something") {
    call.respond(HttpStatusCode.NotFound)
}
Hum I think it’s because i use jetty. I’ll send you the sample project.
There is a sample issue project If you run the project with
./gradlew :jettyRun
you can reproduce the problem. But if you launch the project via the main function in the IDE, then the behavior will be normal.
a
This is probably a bug in the
ServletApplicationEngine
. As a workaround, please add the following code in the
Application.module
definition:
Copy code
fun Application.module() {
    intercept(ApplicationCallPipeline.Fallback) {
        if (call.isHandled) return@intercept
        val status = call.response.status() ?: HttpStatusCode.NotFound
        call.respond(status)
    }
    configureRouting()
}
j
Thank you very much, the workaround works. Do I have to post a bug issue on YouTrack to follow the issue if it is a bug? Or is it me who implements ktor badly?
a
I am going to file it soon
Actually, there is an existing issue about this problem.
895 Views