我定义了数据类,配置了 gson 并创建了路由来处理发布请求,如下所示:
data class PurchaseOrder(val buyer: String, val seller: String,
val poNumber: String, val date: String,
val vendorReference: String)
install(ContentNegotiation) {
gson {
setDateFormat(DateFormat.LONG)
setPrettyPrinting()
}
post("/purchaseOrder"){
val po = call.receive<PurchaseOrder>()
println("purchase order: ${po.toString()}")
call.respondText("post received", contentType =
ContentType.Text.Plain)
Run Code Online (Sandbox Code Playgroud)
以下 JSON 在 POST 请求中发送
{
"PurchaseOrder" : {
"buyer": "buyer a",
"seller": "seller A",
"poNumber": "PO1234",
"date": "27-Jun-2018",
"vendorReference": "Ref1234"
}
}
Run Code Online (Sandbox Code Playgroud)
输出显示所有空值。
purchase order: PurchaseOrder(buyer=null, seller=null, poNumber=null,
date=null, vendorReference=null)
Run Code Online (Sandbox Code Playgroud)
从 call.request.receiveChannel() 读取数据确实显示了正确的 JSON。所以我正在接收数据,但 call.receive() 似乎没有产生预期的结果。
手动获取 JSON 并尝试按如下方式创建 …
我正在使用AJAX将JSON提交到django视图。JSON如下所示:
{
"code":"9910203040", // required
"name":"Abc", // required
"payments":[
{
"amount":300, // required
"name":"efg", // required,
"type": 2 // can be empty
},
{
"amount":100,
"name":"pqr",
"type": 3
}
]
}
Run Code Online (Sandbox Code Playgroud)
付款清单可以是任何大小。如何在Django中验证?是否可以使用Django Forms进行验证?如果是Spring,我将创建Request类并在字段上使用注释,但无法弄清楚如何在Django中执行此操作。