Is there a way to create a Url relative to another...
# ktor
s
Is there a way to create a Url relative to another Url? I have a URL: http://www.somedomain.com/page and another relative path:
/someotherpage
but short of dissecting the URLs, there doesn’t seem to be a way to create one like there is in the JDK:
URL(URL("<http://www.somedomain.com/page>"), "/someotherpage")
s
Have you tried the BASE_URL strategy? I.e. set up constants in your codebase for different URLs, e.g.:
Copy code
const val BASE_URL = "<http://www.somedomain.com>"
const val PAGE_URL = "$BASE_URL/page"
const val SOME_OTHER_PAGE_URL = "$BASE_URL/someotherpage"
s
I'm parsing html, so I need to support generic urls
s
Could we do something similar in that case as well? i.e.
Copy code
val BASE_URL = fetchedUrl.substringBefore("/")
val PAGE_URL = BASE_URL + "/page"
And so on?
s
Yes, but at that point I'm just going to use the built in jvm classes. Just would prefer to use the cross platform ktor stuff. Seems like it's not supported
s
I'm not sure, maybe it is supported, these were just my 2 cents. Maybe someone more experienced should be able to help đź‘Ť
r
UrlBuilder(baseUrl).takeFrom(relativeUrl)
should do the trick