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

Akram Bensalem

01/16/2022, 10:02 AM
I want to Upload an image from Android to server as Binary form. the postman code is:
curl --location -g --request POST '<https://svform.herokuapp.com/createUser/{userName}/{mobileNumber}>' \
--form 'profileImage=@"/Users/akram/Documents/14_07_2020-hbtu_kanpur_20509722.jpg"'
So I write a code like this:
Copy code
@OptIn(InternalAPI::class)
suspend fun creatUser(
    context : Context,
    userName: String,
    mobileNumber:String,
    image: Uri,
) = client
    .submitFormWithBinaryData<MemberEntity>(
        url = "$POST_NEW_USER/$userName/$mobileNumber",
        formData = formData {
            append(
                key = "profileImage",
                value = getBinaryFile(context = context, contentUri = image)!!
            )
        },

            ){

        onUpload { bytesSentTotal, contentLength ->
            println("Sent $bytesSentTotal bytes from $contentLength")
        }
   }
I got an error of type :
Serializer for class 'BinaryItem' is not found.
When I add the ContentType, I got another error
Header(s) [Content-Type] are controlled by the engine and cannot be set explicitly
..... Please any help ?
a

Aleksei Tirman [JB]

01/16/2022, 10:10 AM
Please try to use the following code for your data:
Copy code
formData = formData {
    append("profileImage", File("/Users/akram/Documents/14_07_2020-hbtu_kanpur_20509722.jpg").readBytes(), Headers.build {
        append(HttpHeaders.ContentType, "image/jpg")
        append(HttpHeaders.ContentDisposition, "filename=14_07_2020-hbtu_kanpur_20509722.jpg")
    })
}
👍 1
a

Akram Bensalem

01/16/2022, 10:33 AM
I got this error:
Serializer for class 'MultiPartFormDataContent' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
a

Aleksei Tirman [JB]

01/16/2022, 12:06 PM
Did you set an explicit content type?
❤️ 1
a

Akram Bensalem

01/16/2022, 7:01 PM
I found the solution, I was have a default Type of Json Content in the config File. After I delete it it works. Thank you for the help
405 Views