6

I started a small ratpack app in the Groovy console but I couldn't work out from the documentation how to get a hold of json data that has been sent in the request.

@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}"
    }
  }
}

And an example request:

curl -XPOST -H "Content-Type: application/json" -d '{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}' localhost:5050/foo
shmish111
  • 3,697
  • 5
  • 30
  • 52

3 Answers3

10

Ratpack provides a concept known as a Parser that allows you to parse incoming request body to a given type.

In your case, you could parse incoming request body to a JsonNode, or your own type using the ratpack-jackson module. You can find more information here.

Here is your example using the parser provided by the ratpack-jackson module:

@Grab("io.ratpack:ratpack-groovy:0.9.12")     
@Grab("io.ratpack:ratpack-jackson:0.9.12")    

import static ratpack.groovy.Groovy.*         
import ratpack.jackson.JacksonModule          
import static ratpack.jackson.Jackson.jsonNode

ratpack {                                     
  bindings {                                  
    add new JacksonModule()                   
  }                                           
  handlers {                                  
    post("foo") {                             
      def postBody = parse jsonNode()
      render "Hello world! ${postBody.person}"
    }                                         
  }                                           
}                 

Your curl

curl -XPOST -H "Content-Type: application/json" -d '{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}' localhost:5050/foo

Responds as

Hello world! {"name":"Guillaume","age":33,"pets":["dog","cat"]}

I hope this helps!

Dan Hyun
  • 536
  • 3
  • 7
3

The earlier answers are no longer applicable.
Since version 0.9.19, Ratpack returns a Promise. Also, it includes Jackson parsing support. So your example would look like this as of the time of this writing:

context.parse(Jackson.fromJson(Map)).then { data ->
    println data.person
    // do something with person
    context.response.status(201).send()
}

Full docs available here: https://ratpack.io/manual/current/api/ratpack/jackson/Jackson.html#parsing

p.s. One semantic change I made to the example is to return a 201 (created), since it's a POST.

Dan Tanner
  • 2,229
  • 2
  • 26
  • 39
2

request.body.text would give the string format of the JSON body

post("foo") {
  def slurper = new JsonSlurper()
  def result = slurper.parseText( request.body.text )
  render "Hello world! ${result.person}"
}
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • I looked at rat pack because it looks like it could be a great rest service platform. Given this, what would be the idiomatic way to deal with posted json data in groovy? Doing what I did? – shmish111 May 13 '14 at 18:29
  • depends on the implementation. Here you have used JsonSlurper to parse the JSON payload, that can also be done using Gson or any other JSON library. In this case, this is perfectly fine to work with the way you end up with a map representation of the payload. Give a glance at [Ratpack API](http://www.ratpack.io/manual/current/api/) mainly `ratpack.jackson` package if it will be useful. @shmish111 – dmahapatro May 13 '14 at 19:09