Ktor client (submitFormWithBinaryData) failed to upload a single image

Viewed 19

I'm trying to upload an image via KtorClient and it results in the following exception.

Serializer for class 'MultiPartFormDataContent' is not found.
    Mark the class as @Serializable or provide the serializer explicitly.

This is how I get the image in a Jetpack Composable.

val source = ImageDecoder.createSource(context.contentResolver, uri)
val srcBitmap = ImageDecoder.decodeBitmap(source)

This is how I convert the bitmap into byte arrays.

val byteOutputStream = ByteArrayOutputStream()
srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOutputStream)
val stream = byteOutputStream.toByteArray()
api.uploadImage(
     token = token,
     byteArray = stream
)

This is the upload function. The exception occurred inside the formData block.

override suspend fun uploadImage(token: String, byteArray: ByteArray) {
        return client.submitFormWithBinaryData {
            url(Endpoints.UPLOAD_PIC)
            header("Authorization", token)
            formData {
                this.append("photo", byteArray, Headers.build {
                    append(HttpHeaders.ContentType, "image/jpeg")
                    append(HttpHeaders.ContentDisposition, "filename=image.png")
                })
            }
        }
}

Is this issue related to the size of the photo? Please help me resolve this. I've been working on it for two days.

0 Answers
Related