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

ade

03/03/2022, 12:12 PM
I can't find this in the docs or google... say i have a api with url https://exampleapi.com/{param1}/info How do I create a request so that I insert the param1 parameter in a safe url-encoded way? Do I need to encode it manually?
a

Aleksei Tirman [JB]

03/04/2022, 7:44 AM
In Ktor 2.0.0 you can use the Resources plugin to make type-safe requests. Here is an example:
Copy code
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.resources.*
import io.ktor.client.plugins.resources.Resources
import io.ktor.http.*
import io.ktor.resources.*

suspend fun main() {
    val client = HttpClient(Apache) {
        install(Resources)
        defaultRequest {
            host = "<http://httpbin.org|httpbin.org>"
            port = 80
            url { protocol = URLProtocol.HTTP }
        }
    }

    val r = client.get(Status("202"))
    println(r.status)
}

@kotlinx.serialization.Serializable
@Resource("/status/{param1}")
class Status(val param1: String)
a

ade

03/04/2022, 8:02 AM
Thanks, I'll give that a try
2 Views