khttp
khttp copied to clipboard
Proposal: Provide a way to send data in multipart/form-data format
This proposal is about adding a data class FormData(val value: Any, val type: String)
to khttp, and accept it in data
as Map<String, FormData>
. When GenericRequest sees this combination, it sets the Content-Type
header to multipart/form-data; boundary=%s
, and adds the right Content-Type to every part in that multipart request body. If there are some files to be added to the requests, they can be added too, but this will be optional.
Motivation:
On a previous project of mine, we needed to integrate with a system that accepted JSON as long as we send it encoded with multipart/form-data
setting the respected headers.
Looking at GenericRequest.kt, there is almost a way to hack it with khttp by providing data as Map and a files handle that is empty - the only thing missing is an ability to provide the Content-Type for the parts in the Map of the data.
Currently, I have created a workaround as described here: https://kerestey.net/writing/2019-10-26-post-multipart-form-data-using-khttp.html but I'd like to see this resolved in the library itself.
I am certain this needs a little bikeshedding, and maybe there is a better idea of how to handle this out there, therefore the proposal. If there is a consensus on how to resolve this and needs some changes in the library, I would also be happy to try providing a pull request...