Search code examples
kotlinkotlin-multiplatformktor

How to get the host from a URL in pure kotlin Common module


I need to know how to get the host from URL in "pure" Kotlin. It can be through a library but it can't be JVM. That is because, I am writing a Kotlin Multiplatform library completely in Common module that only depends on Ktor. So that I can support every target Ktor does. And using JVM solution means that I would need to support a solution for every target that I offer.

I was trying to get it through Ktor but didn't find a way yet. So that would also be acceptable solution.


Solution

  • Ktor has it's own Url class, and it has all the necessary fields including host.

    io.ktor.http.Url(urlString = "...").host