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

Artem Dracontis
Aug 2 · 1 min read

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:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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