ktor HTTP API 练习
2021-02-04 21:14
标签:string += val call param png move com world 为每个片段添加唯一 id,并为 /snippets 添加一个 DELETE http 动词,以允许通过身份认证的用户删除自己的片段。 首先为每个片段添加唯一 id: 修改新增片段的post请求: 先测试新增片段的请求是否正常,ID正确 添加一个DELETE的HTTP请求测试 返回结果如下,id为1的snippets已被删除 ktor HTTP API 练习 标签:string += val call param png move com world 原文地址:https://www.cnblogs.com/shy-/p/11456450.html练习一
Question
Answer
data class Snippet(val id: Int, val user: String, val text: String)
val snippets = Collections.synchronizedList(
mutableListOf(
Snippet(id = 1, user = "test", text = "hello"),
Snippet(id = 2, user = "test", text = "world")
)
)
authenticate {
post {
val post = call.receive
添加删除片段的delete请求:authenticate {
delete("/{id}") {
val id = call.parameters["id"]
val snip = snippets.find { s -> s.id == id?.toInt() }
if (snip != null) {
snippets.remove(snip)
call.respond(mapOf("snippets" to synchronized(snippets) { snippets.toList() }))
}
else{
call.respond(mapOf("msg" to "no such id"))
}
}
}
DELETE {{host}}/snippets/1
Authorization: Bearer {{auth_token}}