Is there a way to define a base URL in HttpClient ...
# ktor
Is there a way to define a base URL in HttpClient ? By "base URL" I mean that all requests are based on it, for example : with
= "" when calling
, then the resolved URL is
The easiest way might be to add an extension function on the HTTP client which invokes get() but prefixes the path with the base URL that you want
👍 1
If you want to do it a more complictaed way you might need to come up with your own “base URL” feature and install it to the client
It’s not quite the syntax you want, but you can make your other requests
httpClient.get { url { encodedPath = "endpoint" } }
👍 1
I found that I could archive something close to what I want with the DefaultRequest feature :
Copy code
val client = HttpClient(OkHttp) {
    defaultRequest {
        url {
            protocol = URLProtocol.HTTPS
            host = "<|>"

client.get<HttpResponse>(path = "api/v1/endpoint")
With that I can at least specify a common host. Would be nice to have something similar to Retrofit's
though. I'll consider writing a feature for that.