In Ktor Server, we can get the path of a resource ...
# ktor
m
In Ktor Server, we can get the path of a resource by calling:
href(MyResourceDTO())
. Is there a way to do this in Ktor Client also? I want to avoid hardcoding the paths.
h
The UrlBuilder.href function is part of the shared module, so it is available in the client too.
👍 2
m
You’re right. So I declared the resource like this:
Copy code
@Resource("login")
class LoginRequest
but
href(LoginRequest())
is giving me error
None of the following candidates is applicable:
Copy code
None of the following candidates is applicable:
fun <reified T : Any> HttpClient.href(resource: T): String
fun <reified T : Any> HttpClient.href(resource: T, urlBuilder: URLBuilder): Unit
I imported
import io.ktor.client.plugins.resources.href
on Ktor Client 3.0.1
a
You need to import the
href
function from the
io.ktor.resources
package and pass the resource format as the first argument:
Copy code
import io.ktor.resources.*
import io.ktor.resources.serialization.ResourcesFormat

val str = href(ResourcesFormat(), LoginRequest())
println(str)
1
m
Thanks Aleksei