3

Code below is to upload an file using ktor and kmm ...

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}")
            })
        }
    )
dawoud
  • 104
  • 1
  • 6
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 23 '22 at 13:10
  • i use ktor with kotlin multiplatform exactly i write ktor code in shared module – dawoud Feb 24 '22 at 10:05

2 Answers2

5

You can't do that using the submitFormWithBinaryData method. Use the post or request method. Here is an example:

client.post("https://api.telegram.org/bot<token>/sendDocument?chat_id=$chatId") {
    header("custom", "value")
    body = MultiPartFormDataContent(formData {
        append("document", file.readBytes(), Headers.build {
            append(HttpHeaders.ContentDisposition, "filename=${file.name}")
        })
    })
}
Aleksei Tirman
  • 4,658
  • 1
  • 5
  • 24
  • thanks for answering but when i use your code it return this error /*kotlinx.serialization.SerializationException: Serializer for class 'MultiPartFormDataContent' is not found. Mark the class as @Serializable or provide the serializer explicitly.*/ – dawoud Feb 24 '22 at 10:08
  • Do you send an explicit `Content-Type` header with a value `application/json`? – Aleksei Tirman Feb 24 '22 at 11:55
  • no i will comment with code i written now – dawoud Feb 24 '22 at 12:30
  • You have the line `contentType(ContentType.Application.Json)` that sends the `Content-Type` header with `application/json` value. This causes the SerializationException because the `JsonFeature` tries to serialize `MultiPartFormDataContent` object. To make it work remove that line. – Aleksei Tirman Feb 24 '22 at 12:52
-1

this is my code ...

     val response = client.post<String>("${PublicData.BASEURL}"+"classes/UserFiles"){
                headers {
                    append("X-Parse-Application-Id", PublicData.Application_Id )
                    append("X-Parse-REST-API-Key", PublicData.REST_API_Key)
                    append("Content-Type", "application/json")
                }
    
                contentType(ContentType.Application.Json)
                body =  MultiPartFormDataContent(formData {
                    headersOf("X-Parse-Application-Id", PublicData.Application_Id)
                    headersOf("X-Parse-REST-API-Key", PublicData.REST_API_Key)
                    append("file_type", "Ktor klogo")
                    append("encryption_tool_id", "Ktorkk logo")
                    append("user_id", "Ktor kklogo")
                    append("query", "Ktor kklogo")
                    append("file", file, Headers.build {
                        append(HttpHeaders.ContentType, ContentType.Application.Json)
                        append(HttpHeaders.ContentDisposition, "filename=asd")
                    })
                })
    
            }
dawoud
  • 104
  • 1
  • 6
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 24 '22 at 22:48