Hi, I’m trying out Ktor and want to configure som...
# ktor
d
Hi, I’m trying out Ktor and want to configure some routes with
Locations
I have a problem configuring a simple
POST
endpoint. I have the following code:
Copy code
@Location("/v1/login")
class Login

post<Login> {
 ...
}
But I get 404s. If I change it to
get<Login> {…}
everything is ok. Can’t seem to find any examples using
post
and
Locations
d
Are you sure the client is posting?
d
Yeah, 100%. It works ok if I don’t use Locations.
a
Please check that you use
post
from the
io.ktor.locations
package.
d
I checked. I do. Otherwise post<T> would’ve thrown a compiler error
a
The following server replies with the expected response for `curl -v -X POST http://localhost:8080/v1/login`:
Copy code
import io.ktor.application.*
import io.ktor.locations.*
import io.ktor.response.*
import io.ktor.routing.routing
import io.ktor.server.engine.*
import io.ktor.server.netty.*

@OptIn(KtorExperimentalLocationsAPI::class)
fun main(args: Array<String>) {
    embeddedServer(Netty, port = 8080) {
        install(Locations)
        routing {
            post<Login> {
                call.respondText { "post" }
            }
        }
    }.start()
}

@Location("/v1/login")
class Login
d
Thanks. I have no idea what I had wrong. But I just started from scratch and everything is ok now.
d
Fascinating
d
Ok, I think I figured it out, because it happened again. “Optimise imports” in IDEA cleared the correct
.<http://locations.post|locations.post>
import, for some reason