Hello All, Im trying to figure out, how i can upl...
# ktor
b
Hello All, Im trying to figure out, how i can upload multi records using below example in ktor. I dont see this possiblity using
call.receive
Example json:
{Key:[{Var:value, var:value},{Var:value, var:value}]}
Any idea how i can handle more that one in single route? Also did its possible to combine two keys in one route?
a
I don't think I fully understood your problem but if
value
is a string then receiving works with the following code:
Copy code
fun main() {
    embeddedServer(Netty, port = 8080) {
        install(ContentNegotiation) {
            json()
        }

        routing {
            post("/json") {
                val r = call.receive<Request>()
                println(r)
            }
        }
    }.start()
}

@Serializable
data class Request(val Key: List<Entry>)
@Serializable
data class Entry(@SerialName("Var") val var1: Value, @SerialName("var") val var2: Value)
typealias Value = String
b
I need to insert two types of objects in one route. These objects have diffrent values(String, Int, boolean) I have object1 and few object2. Objects one is document header and object2 are single lines of document body. How i should handle it? I have postgres db and when i need post new single object i use call.receive<myDataClass> and pass return value to insert fun()
a
Could you please share a valid JSON example? How do you determine which type of object to receive if there could be two of them?
b
Example JSON:
{"Header": {
    
"OrderNo":"someInt",
    
"Created by":"someString",
    
"Company":"someString",
    
"Status":"someString",
    
"CreateDate":"someLong",
    
"EstimateDate":"someLong",
    
"EndDate":"someLong",
    
"Assigned to":"someString",
    
"Priority":"someBool",
    
"Reference":"someString"},
"Body1":{
    
"headerID":"someInt",
    
"Item":"someString",
    
"Quantity":"someInt",
    
"Position":"someString",
    
"Warehouse":"someString",
    
"Confirmation":"someBool"},
"Body2":{
    
"headerID":"someInt",
    
"Item":"someString",
    
"Quantity":"someInt",
    
"Position":"someString",
    
"Warehouse":"someString",
    
"Confirmation":"someBool"
    
}}
I didnt define it yet, the point is that I looking for way to define it in proper way and handle it in single route if it's possible. Only idea that i have is to close object in key like on example, but even if its valid I'm still dont know how to receive it and split to separed objects.
a
Please correct me if I am wrong, the
Header
is the details of an order and each
Body
element is the details of an item in that order. If so then I suggest making an array for a list of items:
Copy code
{
  "Header": {
    "OrderNo": "someInt",
    "Created by": "someString",
    "Company": "someString",
    "Status": "someString",
    "CreateDate": "someLong",
    "EstimateDate": "someLong",
    "EndDate": "someLong",
    "Assigned to": "someString",
    "Priority": "someBool",
    "Reference": "someString"
  },
  "Items": [
    {
      "headerID": "someInt",
      "Item": "someString",
      "Quantity": "someInt",
      "Position": "someString",
      "Warehouse": "someString",
      "Confirmation": "someBool"
    },
    {
      "headerID": "someInt",
      "Item": "someString",
      "Quantity": "someInt",
      "Position": "someString",
      "Warehouse": "someString",
      "Confirmation": "someBool"
    }
  ]
}
So to deserialize it you will need to have a class for the root JSON object, a class for a header, and a class for an item. Does it make sense?
👍🏻 1
Then you can save them in a database however you want.