Hello. I encountered a bug, whre I'm sending CSV f...
# ktor
r
Hello. I encountered a bug, whre I'm sending CSV file via HttpClient to Ktor server and server receives only part of this file. Client code:
Copy code
val file = File("src/test/resources/peaks.csv")

val headers = Headers.build {
    append(HttpHeaders.ContentType, ContentType.fromFileExtension("csv").first())
    val fileName = "filename=peaks.csv"
    append(HttpHeaders.ContentDisposition, fileName)
}

println("Sending file with ${file.length()}") //Sending file with 50457

val value = file.readBytes()
val formData = formData { append("file", value, headers) }

client.submitFormWithBinaryData("<http://127.0.0.1:8081/achievements/peaks>", formData)
Server code:
Copy code
post("achievements/peaks") {
    val receiveMultipart = call.receiveMultipart()
    val file = receiveMultipart.allFiles().first()

    val csv = file.provider().readText()

    println("Received file with ${csv.length}") // Received file with 4052
}
Only 4k characters from 50k characters were received. It works in same way when I try to send it via http client in Intelij Idea:
Copy code
POST <http://127.0.0.1:8081/achievements/peaks>
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data.csv"
Content-Type: application/csv

...csv content
Using serialization via JSON instead of CSV file works, but I need to know what I'm doing wrong (Ktor version 2.2.1)
a
Can you share the code of the
allFiles()
method?
r
I was sure that allFiles is part of Ktor, but I just realized it is our internal extension 🙂 But still can't see where is bug:
Copy code
suspend fun MultiPartData.allFiles(): List<PartData.FileItem> {

    val fileResults = ArrayList<PartData.FileItem>()

    multipartLoop@ while (true) {
        val part = readPart() ?: break
        when (part) {
            is PartData.FileItem -> {
                fileResults.add(part)
            }
            else -> part.dispose()
        }
    }

    return fileResults
}
But when I use methods from Ktor it still doesn't work:
Copy code
val receiveMultipart = call.receiveMultipart()
val file = receiveMultipart.readAllParts().filterIsInstance<PartData.FileItem>().first()

val csv = file.provider().readText()

println("Received file with ${csv.length}") // Received file with 4052
a
This is a bug in the
readText
method. As a workaround, you can read all bytes and then decode them into a string:
Copy code
val csv = String(it.provider().readBytes())
r
Thanks! I didn't expect bug in method like this. Workaround did a trick