-
Notifications
You must be signed in to change notification settings - Fork 4
The Request object
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
Depending of the language used Groovy, Javascript or Python, the headers field of the request can be respectively a Map an object or a dictionary.
It contains the HTTP headers sent by the client.
$ curl -H "Accept: application/json" -H "Content-type: application/xml" http://localhost:8080/rest/test rest.get('/test').then { request ->
println request.headers['Accept'] // application/json
println request.headers['Content-type'] // application/xml
} rest.get('/test').then(function(request) {
hprint request.headers['Accept'] // application/json
hprint request.headers['Content-type'] // application/xml
}) def handleGetTest (request):
print request.headers['Accept'] # application/json
print request.headers['Content-type'] # application/xml
rest.get('/test').then(handleGetTest)The URI defined as get/post/put/delete methods param can be a template with substitution variables.
A URI Template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI.
The pathParams field gives a convenient access to these variables.
rest.get('/records/{band}/songs/{id}')The bandand id variables will be evaluated at runtime and injected in the pathParams map.
$ curl http://localhost:8080/rest/records/stranglers/songs/1 rest.get('/records/{band}/songs/{id}').then { request ->
println request.pathParams.band // stranglers
println request.pathParams.id // 1
request.pathParams.band + ' ' + request.pathParams.id
}requestParams object field contains parameters passed by the client in the URL.
$ curl http://localhost:8080/rest/records/search?band=neworder&year=1981&year=1982
rest.get('/records/search').then(function(request) {
hprint (request.requestParams.band[0]) // neworder
hprint (request.requestParams.year[0]) // 1981
hprint (request.requestParams.year[1]) // 1982
return 'Movement, Power, Corruption and lies'
})The last field of the request object is requestBody. It contains the payload sent by the client with the HTTP request.
Depending of the Content-Type HTTP header fixed by the client, the payload will be converted to the appropriate data type
In absence of Content-Type or if text/plain is defined, no data conversion is operated. The requestBody field will have a plain String value
$ curl -d 'Everything goes green' http://localhost:8080/rest/plain rest.post('/plain').then { request ->
println request.requestBody // Everything goes green
}The requestBody field will contain a Java byte[] array containing binary request body.
If the client define a JSON content type in his request, the payload will be automatically converted to a Map/Object/Dictionary in Groovy/Javascript/Python
$ curl -d '{"album":"Black And White", "band": "The Stranglers"}'
-H 'Content-Type: application/json'
http://localhost:8080/rest/albums def handleAlbumPosts(request):
data = request.requestBody
print data['album'] # Black And White
print data['band'] # The Stranglers
rest.post('/albums').then(handleAlbumPosts)XML payloads will be converted but in different formats depending of the programming language used.
Considering the following client call:
$ curl -d '<?xml version="1.0" encoding="utf-8" ?><album name="La folie"></album>'
-H 'Content-Type: application/xml'
http://localhost:8080/rest/albumsIn Groovy, an XML payload will be converted to a XmlSlurper
rest.post('/albums').then { request ->
def album = request.requestBody
print album.@name // La folie
}In Javascript, XML payloads will be parsed and converted to E4X JS XML extension objects.
rest.post('/albums').then(function(request){
var album = request.requestBody
print album.@name // La folie
})In Python, XML payload will be parsed with the standard Java DOM API. A Document instance will be store in the requestBody field.
def handleAlbumsPost(req):
document = req.requestBody
print document.getElementsByTagName('album').item(0).getAttribute('name') // La folie
return ""
rest.post('/albums').then(handleAlbumsPost)