Hi, I have a problem with uploading a file, it sea...
# ktor
r
Hi, I have a problem with uploading a file, it seams like Ktor is not including key in request body. More info 🧵
I have code like this
Copy code
val formData = formData {
            append("file", data, headers)
        }
val multipart = MultiPartFormDataContent(formData)

val response = httpClient
            .post(url) {
                setBody(multipart)
            }
and it looks like this from logger
Copy code
METHOD: HttpMethod(value=POST)
COMMON HEADERS
                                                                                                    -> Accept: application/json
                                                                                                    -> Accept-Charset: UTF-8                                                                                                   
                                                                                                    CONTENT HEADERS
                                                                                                    -> Content-Length: 7697
                                                                                                    -> Content-Type: multipart/form-data; boundary=78282c352c930955-90680557dab423d-6be60bfd-4b7e92bb-5b9073585afbc3f251c
                                                                                                    BODY Content-Type: multipart/form-data; boundary=78282c352c930955-90680557dab423d-6be60bfd-4b7e92bb-5b9073585afbc3f251c
                                                                                                    BODY START
                                                                                                    [request body omitted]
                                                                                                    BODY END
and I need to be like this in postman
where the key
file
is present in request body
the server returns error
Required part 'file' is not present.
which is exactly the same as if I omit the key in postman
like this
a
Can you add the
Content-Disposition
header with the
filename
like in this example?
r
i did with header
Content-Disposition
with
"name=\"file\""
let me try
filename
ive tried that but cant see the header in the request 🤔
Copy code
REQUEST: https://...
                                                                                                    METHOD: HttpMethod(value=POST)
                                                                                                    COMMON HEADERS
                                                                                                    -> Accept: application/json
                                                                                                    -> Accept-Charset: UTF-8
                                                                                                                                                                                                                                                                                                 CONTENT HEADERS
                                                                                                    -> Content-Length: 7721
                                                                                                    -> Content-Type: multipart/form-data; boundary=-1ceef7fe259f1c04-56c9a7c81859744-56e52e78-ebfe80f-24b311ef-72e3f49547
                                                                                                    BODY Content-Type: multipart/form-data; boundary=-1ceef7fe259f1c04-56c9a7c81859744-56e52e78-ebfe80f-24b311ef-72e3f49547
                                                                                                    BODY START
                                                                                                    [request body omitted]
                                                                                                    BODY END
basically i need to send this kind of request
Copy code
curl -X POST "<http://localhost:8080/api/files/upload>" \
     -H "Content-Type: multipart/form-data" \
     -F "file=@/Users/username/Documents/myfile.txt"
but can't figure out how to include the key
file
Finally figured it out. It needed both Content-Disposition - name and filename and also content-type of the file
a
Copy code
client.post("<http://localhost:8080/api/files/upload>") {
    setBody(MultiPartFormDataContent(
        formData {
            append("file", File("/Users/username/Documents/myfile.txt").readBytes(), Headers.build {
                append(HttpHeaders.ContentType, "text/plain")
                append(HttpHeaders.ContentDisposition, "filename=\"myfile.txt\"")
            })
        }
    ))
}
👍 1
r
thx, yep the Content-type was crucial part that was missing in my previous code