request.body.text (0.9.17+)

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

request.body.text (0.9.17+)

dennytron
Greetings!

I'm fairly new to Ratpack. I'm finding it intuitive and have used it to develop a simple REST API.

I'm using Groovy and IntelliJ for my development. I recently decided to upgrade from Ratpack version 0.9.16 to 0.9.19, and I've run into some trouble.

The issue I've encountered is that in 0.9.16, to convert put request body to JSON, I would perform something similar to the following:

put {
    JsonSlurper slurper = new JsonSlurper()
    Map data = slurper.parseText(request.body.text)
    // do something with data
}

In version 0.9.19, text is no longer a property of a request.body.

Most examples I've seen on the internet that show Ratpack parsing a JSON request use the above syntax or something similar. I've read through the latest "example-books" and other code on https://github.com/ratpack, but (as far as I've found) I haven't seen an example of what I'm trying to do. I've noticed a change to using "Promises" in the latest releases, which is a concept I'm still learning. It looks like the body is now a Promise object.

Is there an equivalent, intuitive way to write the above put and body parse in newer versions of Ratpack?

Thanks!
Dennis
Reply | Threaded
Open this post in threaded view
|

Re: request.body.text (0.9.17+)

danthegoodman
Body parsing was made asynchronous in v0.9.19, so you'll interact with the body like so:

put {
   request.body.then { body ->
      Map data = slurper.parseText(body.text)
      // do something with data
  }
}

BTW, 9.19 also comes with jackson support built in, so you can also parse the data like so:

put {
  context.parse(Jackson.fromJson(Map)).then { data ->
    // do something with data  
  }
}

Also, version 1.0.0-rc-1 just came out today. If you're in the process of upgrading, I recommend upgrading all the way.
Reply | Threaded
Open this post in threaded view
|

Re: request.body.text (0.9.17+)

dennytron
Dan, thanks very much for the quick response and the help! That works perfectly. Jackson looks like the way to go as well.

Cheers!
Dennis
rus
Reply | Threaded
Open this post in threaded view
|

Re: request.body.text (0.9.17+)

rus
In reply to this post by dennytron
Hi Dennis!  Yes, the request body is now returned as a Promise.  You could do something like

        request.body.map {
            slurper.parseText(it.text)
        }.then {
            // whatever you need to do here
        }

It depends what you're trying to do really.  If there is more processing of the json after it's parsed you may choose to chain more promise operations for this rather than do lots in the `then` block.  Feel free to post back with your particular use case if you need more help.  Also, the Slack channel is a good source of help http://slack-signup.ratpack.io/

Here are some good resources on Promises:
    http://ratpack.io/manual/current/api/ratpack/exec/Promise.html
    http://beckje01.com/blog/2014/09/10/ratpack-promise/
    http://ldaley.com/post/97376696242/ratpack-execution-model-part-1
    https://objectpartners.com/2015/05/12/my-first-ratpack-app-what-i-learned/
rus
Reply | Threaded
Open this post in threaded view
|

Re: request.body.text (0.9.17+)

rus
Looks like Dan beat me to it :)
Reply | Threaded
Open this post in threaded view
|

Re: request.body.text (0.9.17+)

dennytron
In reply to this post by rus
Hi Rus, very awesome! It looks like that implementation would fit my need as well. Thanks for the resources. I'll start reading up on Promises.

Thanks!
Dennis