https://kotlinlang.org logo
Title
o

Osman Saral

12/24/2021, 10:34 PM
Hello, Is there a way to override
defautlRequest
for some requests?
We have single host for almost every endpoint we have.
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.
suspend fun HttpClient.getOther(): Other = get("<https://example.com/other>")
it sends request to
<https://myapi.com/other>
(it appends the path)
o

Osman Saral

12/25/2021, 11:52 AM
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

Big Chungus

12/25/2021, 11:53 AM
Doesn't it? HTTPClient factory would allow you to set http host each time a client us requested
o

Osman Saral

12/25/2021, 11:56 AM
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

Big Chungus

12/25/2021, 11:56 AM
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

Osman Saral

12/25/2021, 12:01 PM
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

Big Chungus

12/25/2021, 12:03 PM
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

Osman Saral

12/25/2021, 12:06 PM
I set my default host like this on the factory:
defaultRequest {
    url {
        protocol = URLProtocol.HTTPS
        host = "<http://myapi.com|myapi.com>"
        
        encodedPath = "apiPath$encodedPath"
    }
}
And I have an extension function to define my endpoints.
suspend fun HttpClient.getOther(): Other = get("<https://example.com/other>")
request is sent to
<https://myapi.com/apiPath/other>
b

Big Chungus

12/25/2021, 12:07 PM
Ah, that's the problem!
o

Osman Saral

12/25/2021, 12:07 PM
Yes thank you 😄
b

Big Chungus

12/25/2021, 12:08 PM
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

Osman Saral

12/25/2021, 12:09 PM
I tried that too 😞 It didn't work. You're talking about this right?
suspend fun HttpClient.getOther(): Other = get {
    url {
        host = "<http://example.com|example.com>"
        path("other")
    }
}
or this:
suspend fun HttpClient.getOther(): Other = get {
    host = "<http://example.com|example.com>"
    url {
        path("other")
    }
}
b

Big Chungus

12/25/2021, 12:12 PM
I was talking about your second example
Looks like a regression. Raise a bug.
o

Osman Saral

12/25/2021, 1:37 PM
I didn't try the second one actually. Does it matter if it worked? Shouldn't other ones work too?
b

Big Chungus

12/25/2021, 1:42 PM
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

Osman Saral

12/25/2021, 1:43 PM
okay, i will file a bug anyway. thanks
👍 1