我在 Groovy 控制台中启动了一个小型 ratpack 应用程序,但我无法从文档中了解如何获取已在请求中发送的 json 数据。
@Grab("io.ratpack:ratpack-groovy:0.9.4")
import static ratpack.groovy.Groovy.*
import groovy.json.JsonSlurper
ratpack {
handlers {
get {
def slurper = new JsonSlurper()
def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
render "Hello world! ${result.person}"
}
post("foo") {
def slurper = new JsonSlurper()
def result = slurper.parseText("WHAT DO i PUT HERE?")
render "Hello world! ${result.person}"
}
}
}
还有一个示例请求:
curl -XPOST -H "Content-Type: application/json" -d '{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}' localhost:5050/foo