最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

client - Ktor Multipart Upload: Unable to Upload Image File to Server - "Unknown form content type: PartData.FileItem&a

programmeradmin1浏览0评论

I'm trying to upload an image to a server using Ktor's submitFormWithBinaryData API, but I'm facing an issue where the server returns a 400 Bad Request error. In addition, the response logs show the error: Unknown form content type: io.ktor.http.content.PartData$FileItem@334102c.

Here’s what I’ve tried so far:

Steps Taken: Creating Multipart Form Data: I’m trying to upload an image file to the server using multipart/form-data. I’ve created a custom function to convert a File object to a PartData.FileItem for use in the request.

Code to Upload Image:

suspend fun updateProfileImage(endPoint: String, imageFile: File): UpdateProfileImageResponse? {
    if (!imageFile.exists() || imageFile.length() == 0L) {
        Log.e("TAG", "Image file does not exist or is empty")
        return null
    }
    return try {
        val curlHeaders = mutableListOf<String>()
        val response: HttpResponse = client.submitFormWithBinaryData(
            url = "https://$endPoint",
            formData = formData {
                append("image", imageFile.asMultipartPart()) // Correctly append the file part
            }
        ) {
            headers {
                val headersMap = mutableMapOf<String, String>()
                appendRequestHeaders(headersMap, isImageAPI = true)
                append(HttpHeaders.ContentType, "multipart/form-data")
                headersMap.forEach { (key, value) -> 
                    append(key, value) 
                    curlHeaders.add("-H \"$key: $value\"")
                }
            }
        }

        if (response.status.isSuccess()) {
            val responseBody = response.body<String>()
            json.decodeFromString<UpdateProfileImageResponse>(responseBody)
        } else {
            null
        }
    } catch (e: Exception) {
        e.message?.logE("uploadProfileImage")
        null
    }
}

fun File.asMultipartPart(): PartData {
    return PartData.FileItem(
        provider = { this.inputStream().asInput() }, // Correct way to send file
        dispose = { /* No-op */ },
        partHeaders = Headers.build {
            append(HttpHeaders.ContentDisposition, "form-data; name=\"image\"; filename=\"${[email protected]}\"")
            append(HttpHeaders.ContentType, "image/jpeg") // Correct Content-Type
        }
    )
}

Problem: The request is not being recognized properly, and the server returns a 400 Bad Request error. Additionally, the log shows the error: Unknown form content type: io.ktor.http.content.PartData$FileItem@334102c.

Things I’ve Tried: Set Headers Properly: I set the Content-Type header to multipart/form-data and the specific content type for the file (image/jpeg).

File Validation: I check if the file exists and isn’t empty before uploading it.

Multipart File Handling: I’m using the submitFormWithBinaryData method, but the server isn’t able to correctly interpret the form data, especially the file part.

Error Details: Unknown form content type: io.ktor.http.content.PartData$FileItem@334102c The server responds with a 400 Bad Request status code.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论