Hey. I'm new to Kotlin and Ktor. I'm trying to get...
# ktor
z
Hey. I'm new to Kotlin and Ktor. I'm trying to get my uploading function to work but it reads only the first 4096 bytes. I can't figure out what I'm doing wrong and Google doesn't help too. My code:
Copy code
val bytes = ByteArrayOutputStream()
val multipart = call.receiveMultipart()
multipart.forEachPart { part ->
     if (part is PartData.FileItem) {
          val stream = part.streamProvider()
          var data = stream.read()
          while (data != -1) {
               bytes.write(data)
               data = stream.read()
         }
         stream.close()
     }
     part.dispose()
}
e
Hi, @ZargorNET. Thanks for the report. Client or server? Could you provide some additional request information?
z
It's server side. So basically I want to create an image-uploading service for myself. For that I've added a route
Copy code
post("/u") {
. In that route I'm asking for an authentication header(just a simple string password) so that just I can upload images:
Copy code
if (call.request.header("Auth") != configObj.uploadPw) {
                    call.response.status(HttpStatusCode.Unauthorized)
                    call.respondText("""{"error": "unauthorized"}""", ContentType.Application.Json)
                    return@post
                }
And then I'm trying to get all bytes from the uploading images with the method above. My request is a POST request with the auth header included. The body is "form-data". And in the body I'm creating a field named "file" with the specific image I want to upload
👀 1
e
by
form-data
you mean
multipart/form-data
?
z
Yep. Here's the request in text form
Copy code
POST /u HTTP/1.1
Host: 127.0.0.1:8089
Auth: mypassword
Cache-Control: no-cache
Postman-Token: 9eaafc43-81e9-4620-9136-3dd625a8bfac
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="400px-KeyboardLayout-Danish.png"
Content-Type: image/png


------WebKitFormBoundary7MA4YWxkTrZu0gW--
e
@cy