Hi Guys, I am working. with compose multiplatform ...
# compose-ios
b
Hi Guys, I am working. with compose multiplatform and while making a network call using ktor, I'm getting this exception on ios. It's working fine on android and I'm getting the desired response. com.example.network.KtorNetworkError: kotlin.IllegalStateException: Invalid url:
Copy code
@Throws(Throwable::class)
suspend inline fun <reified T> request(
    endpoint: String,
    headers: Map<String, String>? = null,
    queryParams: Map<String, String>?,
    requestBody: Any?,
    method: HttpMethod
): Result<NetworkResponse<T>> {
    try {
        val response = httpClient.request {
            this.method = method
     
            url {
                this.protocol = URLProtocol.HTTPS
                this.host = endpoint 
            }
           
            requestBody?.run {
                setBody(this)
            }
            contentType(ContentType.Application.Json)
            accept(ContentType.Application.Json)
            queryParams?.forEach {
                parameter(it.key, it.value)
            }
        }
Here is more about httpClient that I'm making
Copy code
val httpClientProvider = HttpClientProviderImpl()
open val httpClient: HttpClient by lazy {
    httpClientProvider.httpClient {
        install(ContentNegotiation) {
            json(
                Json {
                    prettyPrint = true
                    isLenient = true
                }
            )
        }
    }
}
Copy code
class HttpClientProviderImpl : HttpClientProvider {

    override fun httpClient(config: HttpClientConfig<*>.() -> Unit): HttpClient {
        return HttpClient()
    }
}

interface HttpClientProvider {
    fun httpClient(config: HttpClientConfig<*>.() -> Unit): HttpClient
}
p
Make sure you are passing the proper url, that what the exception is saying, you are calling a bad url
b
The url is correct as it works fine on android. Here is the url that I'm passing
Copy code
endpoint = "<http://api.github.com/users/mralexgray/repos|api.github.com/users/mralexgray/repos>",
p
I meant to say to make sure that you are passing it correctly. But if you verify that the endpoint is correct then I don't see anything possibly wrong with that code.
c
/users/mralexgray/repos
shouldn’t be part of the
host
if that is
endpoint
you are assigning. Maybe iOS engine is more strict then the one on Android. I’d rather use a url parser and split up the endpoint to all the required parameters.
🙌 1
👍 1
also it’s not an #compose-ios issue you are having - you’d better ask in #ktor
👍 2
b
Yepp, this was only the issue. When i break the url properly into host and segments like removing "/users/mralexgray/repos" from the host then it works fine on ios. Although the url created at the end is same but on ios it works only if you create the url properly with host and path separated. On Android it works fine either way.
Copy code
val segments = ("users" + "mralexgray" + "repos").urlToSegment()
val response = httpClient.request {
    url {
        this.protocol = URLProtocol.HTTPS
        this.host = endpoint //insert host here
        this.path(*segments.toTypedArray())
       
    }
s
@Bhavya Sikka can you please tell, whats
urlToSegment()
basically?
because i'm not able to find it.
b
Heyy @Shoaib khalid sorry missed your msg. urlToSegment() is basically custom method that I created
Copy code
fun String?.urlToSegment(): List<String> {
    if (this.isNullOrBlank()) return listOf()
    return this.split("/")
}