Sending multipart/form-data with Kotlin and Java 11 HttpClient

While building up my own library for Telegram Bot API, I’ve run across the need to transmit files via multipart/form-data. Old established libraries like OkHttp or Apache Http Client already has classes for multipart data, but there is nothing like this in Java 11 new http client.

I’ve discovered a decent collection of recipes for Java 11 http client, and then I’ve used it as the foundation for this article.

Thus I choose to rewrite multipart/form-data method in Kotlin and adapt it to my project:

Here is code for Telegram bot, so you could replace token and try it yourself. Probably, it would be easier to adapt my code to your needs:

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store