Hey channel, Trying to figure out making POST Mul...
# ktor
k
Hey channel, Trying to figure out making POST MultiPartFormData using OkHttpEngine. Looking at curl request executed in swagger where organs are described as
organs array[string] _(formData)
._
Copy code
-H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'images=@IMG_20250327_125636.jpg;type=image/jpeg' \
  -F 'images=@IMG_20250327_125639.jpg;type=image/jpeg' \
  -F 'organs=leaf' \
  -F 'organs=auto'
I’ve arrived at this in ktor.
Copy code
setBody(
    MultiPartFormDataContent(
        formData {
            organTagWithImageAsBytes.entries.forEach { (tag, file) ->
                append("images", file.readBytes(), Headers.build {
                    append(HttpHeaders.ContentType, ContentType.Image.JPEG)
                    append(HttpHeaders.ContentDisposition, "filename=\"${file.name}\"")
                    append("organs", tag)
                })
            }
        }
    )
)
In response I’ve receive default values of organs and not values that were sent. Where doing the call from swagger returns selected tag values. What’s the proper way of appending this data?
a
The problem is that you call the
append("organs", tag)
within the
Headers.build
block, which effectively adds the
organs
header. To solve the problem, move the
append("organs", tag)
call out of the block to append the form part.