kotlin 未找到类“MultipartBody”的序列化程序

u7up0aaq  于 12个月前  发布在  Kotlin
关注(0)|答案(1)|浏览(165)

你好,我正在尝试使用ktor进行多格式请求,但我得到了上面的错误。我如何serilize MultiPartBody?

val body = MultipartBody.Builder().setType(MultipartBody.FORM)
    .addFormDataPart("choice","John Wick")
    .addFormDataPart("image",path,
        File(path).asRequestBody("application/octet-stream".toMediaType()))
    .build()

val httpResponse = client.post {
    url("https://example.com")
    setBody(body)
    header("Content-Type","application/json")
    header("Authorization", "Bearer key")
}

字符串

fzsnzjdm

fzsnzjdm1#

Ktor无法直接发送okhttp3.MultipartBody类的对象。请改用MultiPartFormDataContent类:

val body = MultiPartFormDataContent(
    formData {
        append("choice", "John Wick")
        append("image", File(path).readBytes(), Headers.build {
            append(HttpHeaders.ContentType, "application/octet-stream")
            append(HttpHeaders.ContentDisposition, "filename=\"file\"")
        })
    }
)

字符串

相关问题