Hello, Is there a way to override `defautlRequest`...
# ktor
o
Hello, Is there a way to override
defautlRequest
for some requests?
We have single host for almost every endpoint we have.
Copy code
defaultRequest {
    url {
        protocol = URLProtocol.HTTPS
        host = "<http://myapi.com|myapi.com>"
    }
}
But we have to send a request to a different host for only single endpoint. I've tried to do this, but defautlRequest overrides the host.
Copy code
suspend fun HttpClient.getOther(): Other = get("<https://example.com/other>")
it sends request to
<https://myapi.com/other>
(it appends the path)
o
Thanks @Big Chungus but that didn't really answer my question. @Shan asked it better than me two years ago I think. She also didn't get an answer. I think I will have to try that approach. https://kotlinlang.slack.com/archives/C0A974TJ9/p1585177844026500
b
Doesn't it? HTTPClient factory would allow you to set http host each time a client us requested
o
Okay I get it, but how is it different then setting host for every request. I wanted to set a default host for %90 of my requests. With that approach, I have to create two HTTPClient with the same settings except the host.
b
Because with the factory you can take host as an argument with default value
So if you request a client without specifying host, you'll get default.
Not to mention that you can encapsulate other config in the factory instead of repeating it in all call sites
o
Okay, you're right thanks. I have an Api class which I inject my HTTPClient. I think I have to have a different Api class for other endpoint and inject other HTTPClient there.
But, wouldn't it be better if ktor just didn't ignore my host when I try to request to a full url?
b
Wait, it should respect host config on request instead of default
What are you doing now? Smells like a bug
If you set default host, but then you set the host again on request config the latter should take precedence
I already did that ages ago and it worked back then. Might be a regression with recent versions.
o
I set my default host like this on the factory:
Copy code
defaultRequest {
    url {
        protocol = URLProtocol.HTTPS
        host = "<http://myapi.com|myapi.com>"
        
        encodedPath = "apiPath$encodedPath"
    }
}
And I have an extension function to define my endpoints.
Copy code
suspend fun HttpClient.getOther(): Other = get("<https://example.com/other>")
request is sent to
<https://myapi.com/apiPath/other>
b
Ah, that's the problem!
o
Yes thank you 😄
b
On getOrOther() you need to set host in request config instead of passing it in the url
get() should take a request config lambda arg which should let you override host
o
I tried that too 😞 It didn't work. You're talking about this right?
Copy code
suspend fun HttpClient.getOther(): Other = get {
    url {
        host = "<http://example.com|example.com>"
        path("other")
    }
}
or this:
Copy code
suspend fun HttpClient.getOther(): Other = get {
    host = "<http://example.com|example.com>"
    url {
        path("other")
    }
}
b
I was talking about your second example
Looks like a regression. Raise a bug.
o
I didn't try the second one actually. Does it matter if it worked? Shouldn't other ones work too?
b
I think uri {} is the same as passing full uri in the url arg, so not sure if it should work any different than that, whereas second one overrides host prop explicitly
o
okay, i will file a bug anyway. thanks
👍 1