https://kotlinlang.org logo
#ktor
Title
# ktor
b

bitkid

10/08/2019, 10:32 AM
i am trying to read a csv file from a multipart request and somehow it behaves weird. when i do
Copy code
for (line in part.streamProvider().reader().readLines())
it fails with "Underlying input stream returned zero bytes" but when i do
Copy code
for (line in ByteArrayInputStream(part.streamProvider().readAllBytes()).reader().readLines())
it works as expected. the weird part is when i run "unix2dos file" before uploading it, it works also with the first bit of code. do i need some content type headers or something like this? reading the file locally also works with the first part, so something along the lines
Copy code
for(line in File("bla").reader().readLines())
that's the uploading code
Copy code
protected fun TestApplicationEngine.uploadDataForMatrix(file: File, path: String, method: HttpMethod = HttpMethod.Put): TestApplicationResponse {
        return handleRequest(method, path) {
            val boundary = "***bbb***"
            addHeader(HttpHeaders.ContentType, ContentType.MultiPart.FormData.withParameter("boundary", boundary).toString())
            setBody(boundary, listOf(
                    PartData.FileItem({ file.inputStream().asInput() }, {}, headersOf(
                            HttpHeaders.ContentDisposition,
                            ContentDisposition.File
                                    .withParameter(ContentDisposition.Parameters.Name, "file")
                                    .withParameter(ContentDisposition.Parameters.FileName, "file.txt")
                                    .withParameter(ContentDisposition.Parameters.Size, "${file.length()}")
                                    .toString()
                    ))
            ))
        }.response
    }
when i convert it back with dos2unix it fails again
what is weird that i can read file locally without ktor without problems. seems like the upload is messing with the file somehow
part.streamProvider().readAllBytes() works .. part.streamProvider().reader().readText() fails
111 Views