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

Stylianos Gakis

07/02/2021, 2:40 PM
When using the Locations plugin, is there a way to specify the body variables instead of just the query parameters? For example I want to have something like this
Copy code
@Location("/login")
data class PostLogin(val username: String, val password: String)
But I want the
username
and the
password
to be part of the body of the post request instead of the Params. Is that something that is possible, or must I do
call.receive
like I would when I wasn’t using the Locations plugin?
n

nschulzke

07/02/2021, 3:33 PM
No, there is not, which is one major weak point in the locations plugin. You can write your own without too much difficulty. I just did it myself to solve a few issues I had with the official plugin, and while I can't release the code yet, I can give you some pointers if you're interested.
s

Stylianos Gakis

07/02/2021, 3:39 PM
It was mostly for a pet-project I am playing around with simply to learn a bit more about ktor/server side dev. I highly doubt I’m the person to do a deep dive on the matter unfortunately. For now I just removed the Locations and I am trying out using the
<http://Route.post|Route.post>
method that allows you to pass a reified type and it does the
call.receive()
for you allowing me to write a route that looks something like this:
Copy code
@Serializable
data class Login(val username: String, val password: String)

post<Login>("login") { login: Login ->
    val response = authController.login(
        Username(login.username), Password(login.password)
    )
    call.respondWith(response)
}
I guess that’s too bad that I can’t use the Locations plugin, but if it can’t do that for me, I don’t see a reason for me to use it other than a prettier way to define the routes.
n

nschulzke

07/02/2021, 3:44 PM
You can generate URLs from the Locations, which would mean, if you were generating all your URLs server-side, you could rename endpoints by just changing the annotation in one place. However, in most cases, your URLs aren't all going to be generated server-side (they're located in an SPA or a mobile app), so the value there is minimal. A typed multiplatform locations API would be really powerful, as it would allow you to guarantee the correct URLs and request/response bodies are used across all platforms, but it doesn't seem to be a priority for the Ktor team. If I can get the one I built stabilized, I'll see if I can get approval to release it as open-source.
s

Stylianos Gakis

07/02/2021, 3:45 PM
Yeah it sounds like a worthy thing to build. I hope it goes well for you and that you will be able to open-source it!
d

Dominaezzz

07/02/2021, 7:48 PM
There's a POC PR on ktor btw