Uploading files to telegram bot api using ktor

I'm writing wrapper for telegram-bot-api using kotlin and ktor. I have a problem - can't find working way to upload files.

(from tg bot api docs)
There are three ways to send files (photos, stickers, audio, media, etc.):

  1. If the file is already stored somewhere on the Telegram servers, you don't need to reupload it: each file object has a file_id field, simply pass this file_id as a parameter instead of uploading. There are no limits for files sent this way.
  2. Provide Telegram with an HTTP URL for the file to be sent. Telegram will download and send the file. 5 MB max size for photos and 20 MB max for other types of content.
  3. Post the file using multipart/form-data in the usual way that files are uploaded via the browser. 10 MB max size for photos, 50 MB for other files.

With 1-st and 2-nd ways I haven't any problems.

For now I have that ugly function that makes request to tg and parse answer:

internal suspend inline fun <reified T> makeRequest(token: String, method: TelegramMethod, vararg params: Pair<String, Any?>, files: Map<String, String> = emptyMap()): T {
    try {
        val data: List<PartData> = formData {
            files.forEach { key, fileName ->
                append(key, Files.newInputStream(Paths.get(fileName)).asInput())
        val response = client.submitFormWithBinaryData<HttpResponse>(data) {
            this.method = HttpMethod.Post
            url {
                protocol = URLProtocol("https", 42)
                host = API_HOST
                encodedPath = API_PATH_PATTERN.format(token, method.methodName)
                params.forEach { (name, value) ->
                    if (value != null) { this.parameters[name] = value as String }
        val result = response.receive<String>()
        return parseTelegramAnswer<T>(response, result)
    } catch (e: BadResponseStatusException) {
        val answer = mapper.readValue<TResult<T>>(e.response.content.readUTF8Line()!!)
        throw checkTelegramError(e.response.status, answer)

Without files it works, with files - it doesn't. (I think that I doing everything wrong)

Usage example:

suspend fun getUpdates(offset: Long? = null, limit: Int? = null, timeout: Int? = null, allowedUpdates: List<String>? = null): List<Update> =
            "offset" to offset?.toString(),
            "limit" to limit?.toString(),
            "timeout" to timeout?.toString(),
            "allowed_updates" to allowedUpdates

I have test it on different files and I find that:

  1. if I send files between 17,9 KiB and 56,6 KiB I get from tg following error: Bad Request: wrong URL host

  2. if I send files between 75,6 KiB and 913,2 KiB I get error 413 Request Entity Too Large

* I were using sendDocument method

What is true way to send files using ktor?