Hello I’m trying to send a multipart request, the...
# ktor
r
Hello I’m trying to send a multipart request, the problem is whatever is sent in the body it appears empty body Here’s my code
Copy code
HttpClient.submitFormWithBinaryData(
url= url,
formData = formData {
append(“store_image_profile”, data, Headers.build {
append(HttpHeaders.ContentType, “ multipart/form-data”)
append(HttpHeaders.ContentDisposition, “ form-data”)
})
}
)
Any thoughts?
a
Why is the contentType multipart/form-data? Besides the obvious space issues between the quotes. The
submitFormWithBinaryData
already takes care of your multipart contentType In the docs it shows you need to pass the content-type of your file:
Copy code
val response: HttpResponse = client.submitFormWithBinaryData(
    url = "<http://localhost:8080/upload>",
    formData = formData {
        append("description", "Ktor logo")
        append("image", File("ktor_logo.png").readBytes(), Headers.build {
            append(HttpHeaders.ContentType, "image/png")
            append(HttpHeaders.ContentDisposition, "filename=\"ktor_logo.png\"")
        })
    }
)
r
I tried your solution but still the body is empty
a
How do you consume the body?
r
I didn’t get you what do you mean by consume ?!
You mean in the ktor client object it self? Does it requires a specific configuration?
a
How do you validate that the body is empty?
r
From chucker apis log
And the response says it can’t find the parameter
a
Can you try to make the request to https://httpbin.org/post to check if the response actually contains the required form fields?
r
Okay one minute
I just tried how to know what have been sent
I tried https://httpbin.org/post and it shows the body is still empty, even if I tried to send a mock bytearray
URL: https://httpbin.org/post Method: POST Protocol: h2 Status: Complete Response: 200 SSL: Yes Request time: Mon Oct 21 124732 GMT+02:00 2024 Response time: Mon Oct 21 124732 GMT+02:00 2024 Duration: 223 ms Request size: 278 B Response size: 615 B Total size: 893 B ---------- Request ---------- Authorization: Accept: application/json Accept-Charset: UTF-8 User-Agent: Ktor client Content-Type: multipart/form-data; boundary=507968bb5ca74072-5377b56f3ce6c706-769d93dd-3eff87cb-6275d053a9e4d9d-7d (body is empty) ---------- Response ---------- date: Mon, 21 Oct 2024 104733 GMT content-type: application/json content-length: 615 server: gunicorn/19.9.0 access-control-allow-origin: * access-control-allow-credentials: true { "args": {}, "data": "", "files": { "store_img_profile": "\u0000" }, "form": {}, "headers": { "Accept": "application/json", "Accept-Charset": "UTF-8", "Accept-Encoding": "gzip", "Authorization": "", "Content-Length": "278", "Content-Type": "multipart/form-data; boundary=507968bb5ca74072-5377b56f3ce6c706-769d93dd-3eff87cb-6275d053a9e4d9d-7d", "Host": "httpbin.org", "User-Agent": "Ktor client", "X-Amzn-Trace-Id": "Root=1-67163145-492535c90fc0f46e741c1a9d" }, "json": null, "origin": "156.174.41.214", "url": "https://httpbin.org/post" }
the code is here
Copy code
submitFormWithBinaryData(
    url = "",
    formData = formData {
            append("store_img_profile", body, Headers.build {
                append(HttpHeaders.ContentType, "image/png")
                append(HttpHeaders.ContentDisposition, "filename=\"image_file\"")
            })
    }
)
a
The body isn't empty, and it contains the
store_img_profile
multipart form field.
r
But the body is empty
m
I believe she’s referring to the Request body being empty. It could also be as a result of the log library you’re using.
👍 1
Yeah, it’s as a result of the log library. Ktor is sending the image byteArray but it’s omitted from the logs. So your log library is just interpreting it as an empty body
👍 1
r
Thank you @Mofe Ejegi