https://kotlinlang.org logo
#ktor
Title
# ktor
d

dimitar_

01/15/2022, 12:03 PM
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

Dominaezzz

01/15/2022, 12:54 PM
Are you sure the client is posting?
d

dimitar_

01/15/2022, 1:31 PM
Yeah, 100%. It works ok if I don’t use Locations.
a

Aleksei Tirman [JB]

01/15/2022, 2:37 PM
Please check that you use
post
from the
io.ktor.locations
package.
d

dimitar_

01/15/2022, 2:42 PM
I checked. I do. Otherwise post<T> would’ve thrown a compiler error
a

Aleksei Tirman [JB]

01/15/2022, 2:48 PM
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

dimitar_

01/15/2022, 3:14 PM
Thanks. I have no idea what I had wrong. But I just started from scratch and everything is ok now.
d

Dominaezzz

01/15/2022, 3:16 PM
Fascinating
d

dimitar_

01/15/2022, 3:19 PM
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