Skip to content

Writing RESTful services

Solimando Damien edited this page Jun 18, 2015 · 4 revisions

The show.restobject

In a show script, a show.rest object is accessible directly via the rest variable. It let you create REST HTTP endpoints.

Each endpoint is hidden behind the /rest/ path

Example of endpoints URL's

    $ curl http://localhost:8080/rest/companies
    $ curl http://localhost:8080/rest/bands
    $ curl http://localhost:8080/rest/banks

GET/POST/PUT/DELETE methods

The rest object allow the creation of REST HTTP endpoints. The major REST operations are supported via the get,post,put and delete methods. They all take one or more URL paths as arguments.

To handle the corresponding HTTP request, we call the thenmethod on the restobject. It takes a closure as only argument.

Groovy GET example:

    rest.get("/hello").then { request ->
        'GET response from Groovy'
    } 

Javascript POST example:

    rest.post('/pet').then(function(request) {
         return 'POST response from JS'
    })

Python PUT example:

    def handleHello(request):
        return 'PUT response from Python'

    rest.put('/pet').then(handleHello)

Groovy DELETE example:

    rest.delete("/pet").then { request ->
        'DELETE response from Groovy'
    } 

The request callback

The closure handling the HTTP request is the only argument passed to the then method. This closure receives a requestobject as only argument.

The request object has the following fields:

  • headers a map containing HTTP headers sent by the client
  • pathParams a map containing substitution variables from the URL
  • requestParams a map containing URL parameters sent by the client
  • requestBody an object containing the request body

More details on the request object can be found here

The callback response

The callback return object will be the response sent to the client. Multiple return types are supported and automatic response conversion will be performed.

Response conversion will be performed if content negotiation is realized by the client using the Accept HTTP header. In absence of header, a basic toString transformation will be applied by the framework.

For more details, go there

Clone this wiki locally