https://kotlinlang.org logo
#ktor
Title
# ktor
a

Aleksandr Ivanov

08/09/2021, 2:07 PM
I have a problem in my multiplatform project on iOS with InvalidMutabilityException while uploading a file via KTOR. I found an issue which is marked as fixed in 1.6.2, but in my case it doesn’t help. I tried to reproduce my problem based on
client_mpp
example and found that for ByteArray with size 100 problem is not happened, but for size 200 I got an exception. here is the code of modified
ApplicationApi
Copy code
@SharedImmutable
internal expect val ApplicationDispatcher: CoroutineDispatcher

class ApplicationApi {

    // 100 - ok, 200 - kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen <object>
    private val SIZE = 200;

    private val client = HttpClient()

    private val address = Url("<https://cors-test.appspot.com/test>")

    fun about(callback: (String) -> Unit) {
        GlobalScope.launch(ApplicationDispatcher) {
            val result: String = <http://client.post|client.post> {
                url(address.toString())
                val filename = "test"
                val mimeType = "image/png"
                val filePart = formData {
                    val key = "photo"
                    val headers = Headers.build {
                        append(HttpHeaders.ContentDisposition, "filename=$filename;type=$mimeType")
                    }
                    append(key, ByteArray(SIZE), headers)
                }
                body = MultiPartFormDataContent(filePart)
            }

            callback(result)
        }
    }
}
a

Aleksei Tirman [JB]

08/09/2021, 6:21 PM
This is a known issue KTOR-2947 that is, unfortunately, not fixed yet.
a

Aleksandr Ivanov

08/09/2021, 6:22 PM
Thank for the direction.
@Aleksei Tirman [JB] Hi, Do you have any estimations when it will be fixed?
a

Aleksei Tirman [JB]

08/10/2021, 4:19 PM
It will be fixed in Ktor 2.0.0 at the end of October.
a

Aleksandr Ivanov

08/11/2021, 2:22 PM
Ohhhhh, Any suggestion for workaround?
a

Aleksei Tirman [JB]

08/11/2021, 3:29 PM
@e5l do you have any?
6 Views