I’ve tried using the following code, but the results are not as expected. The FormData is missing from the final request.
Is something wrong with how I’m building the HttpRequestBuilder
?
val client = HttpClient()
val request = HttpRequestBuilder().apply {
url("https://httpbin.org/post")
method = HttpMethod.Post
formData {
append("objectType", "2")
}
}
val response = client.request(request)
The problem is that the formData is a pure function that doesn’t mutate a HttpRequestBuilder
but only returns a list of PartData
objects. To send the multipart/form-data
request, instantiate the MultiPartFormDataContent
class by supplying a list of PartData
created by the formData
function and pass that instance to the setBody
method:
val request = HttpRequestBuilder().apply {
url("https://httpbin.org/post")
method = HttpMethod.Post
setBody(
MultiPartFormDataContent(
formData {
append("objectType", "2")
}
)
)
}