-
Notifications
You must be signed in to change notification settings - Fork 4
Writing RESTful services
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
$ curl http://localhost:8080/rest/companies
$ curl http://localhost:8080/rest/bands
$ curl http://localhost:8080/rest/banksThe 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 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:
-
headersa map containing HTTP headers sent by the client -
pathParamsa map containing substitution variables from the URL -
requestParamsa map containing URL parameters sent by the client -
requestBodyan object containing the request body
More details on the request object can be found here
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