Hi guys, I want to use only one HttpClient to requ...
# ktor
c
Hi guys, I want to use only one HttpClient to request multiple hosts. I use type-safe requests @Resource("https://example.com/articles") class Articles() but the result request URL will be HTTP://localhos/https:<//example.com/articles>. I saw https://youtrack.jetbrains.com/issue/KTOR-5586 fixed, but not for resources, only plain request.
a
The
Resource
annotation accepts a path, not a full URL. You can use the DefaultRequest plugin to set the default URL for all requests.
c
Thanks @Aleksei Tirman [JB] , how can I use 1 http client for multiple resources from mutiple host names. without Resource I can do it.
a
You can configure
HttpRequestBuilder
for each request to set the appropriate host. Here is an example:
Copy code
@Resource("/get")
class Get()

suspend fun main() {
    val client = HttpClient(CIO) {
        install(Resources)
    }

    val response =  client.get(Get()) {
        url {
            protocol = URLProtocol.HTTPS
            host = "httpbin.org"
        }
    }

    println(response.bodyAsText())
}
c
I missed this one. I still have a small concern that I need to explicit specify the host ( which I believe is not “type-safe”. I can set a wrong host or forget to set host). I still go with your suggestion. Thank you a lot for your help.