Hi, I'm struggling in trying to send a list of obj...
# ktor
r
Hi, I'm struggling in trying to send a list of objects next to an image file in ktor using
MultiPartFormDataContent
here is my code
Copy code
val body = MultiPartFormDataContent(
    parts = formData {
        append("images", request.image, Headers.build {
              append(HttpHeaders.ContentType, "image/png")
              append(HttpHeaders.ContentDisposition, "filename=\"name.png\"")
        })
                
        request.items.forEachIndexed { index, item ->
            append("items[$index]", Json.encodeToString(item), Headers.build {
                append(HttpHeaders.ContentType, "application/json")
            })
        }
    }
)
runCatchingAllowingCoroutineCancellation {
    <http://httpClient.post|httpClient.post>("receipts") {
        expectSuccess = true
        contentType(ContentType.MultiPart.FormData)
        setBody(body)
    }.body<Unit>()
}
1
the above code generates this request first screenshot (look at
items
) if I encode the whole list together I get the second screenshot's result
while this is how it looks if I send it as a normal serializable body request without
multipart