HttpClient: And how do I get the full url when cal...
# ktor
h
HttpClient: And how do I get the full url when calling href when I also use the defaultRequest plugin?
a
Can you please share a code snippet that illustrates your problem?
h
Well, I want to call
client.href("foo/bar")
with
defaultRequest { url("<https://example.com/root/>") }
and I want to get "https://example.com/root/foo/bar"
a
There is a suboptimal solution of using the client with
MockEngine
to trigger the default request plugin and receive the result as `CompletableDeferred`:
Copy code
suspend fun main(): Unit = coroutineScope {
    val client = HttpClient(MockEngine) {
        install(Resources)
        engine {
            addHandler { respondOk() }
        }
        defaultRequest {
            url("<https://example.com/root/>")
        }
    }

    val urlDeferred = CompletableDeferred<URLBuilder>()
    client.requestPipeline.intercept(HttpRequestPipeline.State) {
        urlDeferred.complete(context.url)
    }

    launch {
        client.get(FooBar())
    }

    println(urlDeferred.await())
}

@Resource("foo/bar")
class FooBar
h
I absolut don't want to use MockEngine inside a library 🙂 But i will take a look at the interception, maybe I can just throw an exception and ignore it?
a
Yes, that will work.