Search code examples
androidkotlin-multiplatformktor

Diffrence between host and baseurl in ktor


I am not able to completely understand the difference between host and baseUrl in ktor. I need to set a baseURL for my project and I have been through this and this youtrack issues but both of them suggests a little different approach and I confused what to choose. I can set host = "baseurl" in defaultrequest and then simply use

httpClient.post<T> {
    url("suffix here")
}

and it works. But am not sure if this is this the correct way and if it is then does it mean host works same as baseURL which we have in retrofit and okhttp.


Solution

  • What you're doing works because of the way that URLBuilder.takeFromUnsafe() is implemented. This function is responsible for parsing your "suffix here" value into a url - it does so by setting only the portions of the url that it can identify in the string you're passing in. See URLParser.kt#28 for more details.

    Is it safe - yes. Is it a good idea? I don't think so. It's implicit and relies on future developers being familiar with the baseUrl approach used by other libraries (okhttp and retrofit).