Search code examples
kotlinkotlin-multiplatformktorktor-client

Ktor upload ByteArray with FormData


I get ByteArray from camera in KMP project and try to upload it to server.

I try this

                        val response = client.post(url) {
                            setBody(
                                MultiPartFormDataContent(
                                    formData {
                                        if (byteArrays != null) {
                                            append("image", byteArrays, Headers.build {
                                                append(HttpHeaders.ContentType, "image/png")
                                                append(HttpHeaders.ContentDisposition, "filename=${name}")
                                            })
                                        }
                                    },
                                )
                            )
                            onUpload { bytesSentTotal, contentLength ->
                                println("Sent $bytesSentTotal bytes from $contentLength")
                            }
                        }

But error "Required request part 'file' is not present"


Solution

  • Looks like you've specified "image" as the key name for the form data. Perhaps try switching it to "file" instead.