https://kotlinlang.org logo
#compose-ios
Title
# compose-ios
b

Bhavya Sikka

12/06/2023, 10:27 AM
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

Pablichjenkov

12/06/2023, 11:38 AM
Make sure you are passing the proper url, that what the exception is saying, you are calling a bad url
b

Bhavya Sikka

12/06/2023, 6:06 PM
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

Pablichjenkov

12/06/2023, 7:24 PM
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

Chrimaeon

12/07/2023, 10:04 AM
/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

Bhavya Sikka

12/08/2023, 5:21 AM
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

Shoaib khalid

12/19/2023, 10:58 AM
@Bhavya Sikka can you please tell, whats
urlToSegment()
basically?
because i'm not able to find it.
b

Bhavya Sikka

01/02/2024, 4:52 AM
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("/")
}
5 Views