How can I post file as multipart/form-data use ktor client? I want to use it for telegram bot API "send document". I need to achieve the same result as the curl command
curl -F document=@"path/to/some.file" https://api.telegram.org/bot<token>/sendDocument?chat_id=<chat_id>
CodePudding user response:
You can use the submitFormWithBinaryData method to send mutlipart/form-data request. Here is a solution:
val client = HttpClient(Apache) {}
val file = File("path/to/some.file")
val chatId = "123"
client.submitFormWithBinaryData(
url = "https://api.telegram.org/bot<token>/sendDocument?chat_id=$chatId",
formData = formData {
append("document", file.readBytes(), Headers.build {
append(HttpHeaders.ContentDisposition, "filename=${file.name}")
})
}
)