Hi everyone, I am using Ktor in Compose Multiplat...
# ktor
s
Hi everyone, I am using Ktor in Compose Multiplatform. Little question about
Headers.build
within a
MultiPartFormDataContent
.
Copy code
return <http://client.post|client.post>("$API_URL//upload") {
            contentType(ContentType.MultiPart.FormData)
            setBody(
                MultiPartFormDataContent(
                    formData {
                        append("type", type)
                        append("file", image, Headers.build {
                            append(HttpHeaders.ContentType, "image/*")
                            append(
                                HttpHeaders.ContentDisposition,
                                "filename=$filename"
                            )
                        })
                    },
                )
            )
        }
My server always return
HTTPException [Error]: Malformed FormData request. Content-Disposition header in FormData part is missing a name.
Well, I tried to make sure
ContentDisposition
is set properly, but when i check the request, it is never send. Did I miss something? Thanks a lot 🙏
a
I guess the problem is that the filename's value isn't surrounded by quotes. Can you try sending the
ContentDisposition
like in this example?
s
Thank you for reply. Yes I have tried from the documentation But it doesn't work either.
Copy code
append("file", image, Headers.build {
       append(HttpHeaders.ContentType, "image/*")
       append(HttpHeaders.ContentDisposition, "filename=\"$filename\"")
 })
Worth to mention that Postman works properly
I can see on the
onUpload
the size sent from the phone and what's receive on the server is same. But I am still getting
HTTPException [Error]: Malformed FormData request. Content-Disposition header in FormData part is missing a name.
a
I suggest comparing both requests made with Postman and Ktor via WireShark to determine the difference.
s
Thanks for the suggestion, I checked in detail and apparently the server mind the quote. Postman
Content-Disposition: form-data; name=*"type"*\r\n
vs Ktor
Content-Disposition: form-data; name=*type*\r\n
I had to add
\"
between the append name.
Copy code
MultiPartFormDataContent(
                    formData {
                        append("\"type\"", type)
Thanks! Don't know if there is any RFC related to this