KTOR Client does not send the full Byte Arrays. Only 76 bytes get uploaded

Viewed 14

I'm using Ktor Client to upload a photo to the server. But the following function only sends 76 bytes according to the log.

suspend fun uploadImage(token: String, byteArray: ByteArray) {

        return client.submitFormWithBinaryData {
            url(MainEndpoints.UPDATE_PROFILE_PIC)
            formData {
                header("Authorization", token)
                append("photo", byteArray, Headers.build {
                    println("Original size ${byteArray.size} bytes")
                    append(HttpHeaders.ContentType, "image/jpg")
                    append(HttpHeaders.ContentDisposition, "filename=image.jpg")
                })
            }
            onUpload { bytesSentTotal, contentLength ->
                println("Sent $bytesSentTotal bytes from $contentLength")
            }
        }
    }

This is the log I get. No matter the image size, it only uploads 76 bytes.

Original size 676755 bytes
Sent 76 bytes from 76

Is this has to do with client configuration? Here's mine.

HttpClient(CIO) {
            install(JsonFeature) {
                serializer = KotlinxSerializer(
                    kotlinx.serialization.json.Json {
                        prettyPrint = true
                        isLenient = true
                        ignoreUnknownKeys = true
                    }
                )
            }
            install(HttpTimeout) {
                requestTimeoutMillis = 15000L
            }
            install(Logging) {
                logger = object : Logger {
                    override fun log(message: String) {
                        Log.i("Logging", message)
                    }
                }
                level = LogLevel.ALL
            }
            install(ResponseObserver) {
                onResponse {}
            }
        }
0 Answers
Related