<https://stackoverflow.com/q/76750729/10334333> C...
# ktor
v
https://stackoverflow.com/q/76750729/10334333 Can someone help me with this 🙏
c
Use “/articles”.
v
But I'm passing Root ("/") as parent to the Articles resource. So it will become "/articles" isn't it?
c
Not for a top level class. See the docs re: nested classes.
v
Copy code
@Resource("/articles")
internal object Articles

@Resource("/article")
internal data class Article(val parent: Articles = Articles)

fun main() {
    embeddedServer(CIO, port = 8080){
        install(Resources)
        routing {
            get<Articles> {
                call.respondText("articles list")
            }
            get<Article> {
                call.respondText("article")
            }
        }
    }.start(true)
}
now it's the same again, it gives 405 for /articles but 404 /articles/article
so only the root path works and all the sub-paths gives wrong status
c
See the docs on nested classes.
v
Copy code
@Resource("/articles")
internal class Articles {
    @Resource("/article")
    internal data class Article(val parent: Articles = Articles())
}

fun main() {
    embeddedServer(CIO, port = 8080){
        install(Resources)
        routing {
            get<Articles> {
                call.respondText("articles list")
            }
            get<Articles.Article> {
                call.respondText("article")
            }
        }
    }.start(true)
}
Ah ok, it should be in a nested class. Thanks, @Chris Lee somehow I overlooked the doc
now it works!🙂
👍 1
Copy code
@Resource("/articles")
class Articles {
    @Resource("{id}")
    class Id(val parent: Articles = Articles(), val id: Long)
}

fun main() {
    embeddedServer(CIO, port = 8080){
        install(Resources)
        routing {
            get<Articles> {
                call.respondText("articles list")
            }
            get<Articles.Id> {
                call.respondText("article123")
            }
        }
    }.start(true)
}
But it doesn't work for the path with parameter, in the above code I get the correct 405
PUT:/artcles
but for
PUT:/articles/12
I get the wrong 404 status again. @Chris Lee do you have any idea?
c
Perhaps
@Resource("/{id}")
- paths always start with a /
v
I tried that first and it doesn't work, that's why I removed the
/
as mentioned in the doc https://ktor.io/docs/type-safe-routing.html#resource_path_param
c
your handles are for
get
- you’ve stated using
PUT
. Try just with
GET
requests firts.
v
yes,
GET
works without any issue (either with or without
/
). My problem is the wrong status code for the method is not defined for the path. I'm expecting 405 Method not allowed but ktor gives 404 Not found
c
is it documented to return a 405? that’s often framework-specific and a bit subjective; there is not a handler that accepts PUT at that url.
v
and now the worst thing is, after this path parameter all subpaths give this same wrong status code. for example
/articles/{id}/article
is it documented to return a 405? that's often framework-specific and a bit subjective; there is not a handler that accepts PUT at that URL.
it's the standard response
https://github.com/ktorio/ktor/issues/649 ktor already has this issue but it's closed for some reason
c
like I said, it’s subjective as to how that should behave. It’s not obviously wrong to return a 404. Why are you expecting a 405? Does that really matter to your solution?
v
yes, because of some spec I need to return 405 otherwise I'm violating the spec.
c
perhaps adjust the spec? requiring 405 is way down in the weeds.