Hi all. I have a form that I receive 3 fields str...
# ktor
r
Hi all. I have a form that I receive 3 fields string int file (pdf). But I'm not sure how to do this with ktor/kotlin. My class
Copy code
data class SomeClass (
name: String,
age: int,
document: ByteArray (is it correct?)
)
fun Route.test() {
 route("/test") {
   post("/doc") {
      val receive = call.receive<SomeClass>()
...
a
You have to upload the PDF first to a/your storage then pass the download link to your API. That is replacing
document
type to
String
(download url)
r
Ok, how would i do that?
a
If you make a
multipart/form-data
request then you can use the ApplicationCall.receiveMultipart method to receive a form content on the server side.
r
@Aleksei Tirman [JB] As a multipart, I just don't inform the object that I will receive between <>, right? it will loop through each key and value and perform the given logic. in the example, it could be
Copy code
data class SomeClass (
name: String,
age: int,
}
Copy code
multipartData.forEachPart { part ->
                when (part) {
                    is PartData.FormItem -> {
                       fileDescription = part.value
                        add each value to someClass class ???
                    }

                    is PartData.FileItem -> {
                        fileName = part.originalFileName as String
                        var fileBytes = part.streamProvider().readBytes()
                        File("uploads/$fileName").writeBytes(fileBytes)
                    }

                    else -> {}
                }
          write someClass to the database
a
Yes.
e
Alternately, you could encode the pfd to base64 string and decode it in the server