Skip to content
Solimando Damien edited this page Jun 18, 2015 · 10 revisions

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

request.headers

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.

Example of client call

    $ curl -H "Accept: application/json" -H "Content-type: application/xml" http://localhost:8080/rest/test

Groovy Show

    rest.get('/test').then { request ->
        println request.headers['Accept']         // application/json
        println request.headers['Content-type']   // application/xml
    }

Javascript Show

     rest.get('/test').then(function(request) {
        hprint request.headers['Accept']         // application/json
        hprint request.headers['Content-type']   // application/xml
     })

Python Show

    def handleGetTest (request):
       print request.headers['Accept']         # application/json
       print request.headers['Content-type']   # application/xml

    rest.get('/test').then(handleGetTest)

request.pathParams

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.

Example of URI template

    rest.get('/records/{band}/songs/{id}')

The bandand id variables will be evaluated at runtime and injected in the pathParams map.

Example of client call

    $ curl http://localhost:8080/rest/records/stranglers/songs/1

Groovy Show

    rest.get('/records/{band}/songs/{id}').then { request ->
         println request.pathParams.band    // stranglers
         println request.pathParams.id      // 1
         
         request.pathParams.band + ' ' + request.pathParams.id
    }

request.requestParams

requestParams object field contains parameters passed by the client in the URL.

Example of client call

 $ curl http://localhost:8080/rest/records/search?band=neworder&year=1981&year=1982

Javascript show

     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'
     })

request.requestBody

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

Content-Type: text/plain

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
     }

Content-Type: application/octet-stream

The requestBody field will contain a Java byte[] array containing binary request body.

Content-Type: application/json

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)

Content-Type: application/xml

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/albums

Groovy

In Groovy, an XML payload will be converted to a XmlSlurper

      rest.post('/albums').then { request ->
          def album = request.requestBody
          print album.@name                      // La folie
      }

Javascript

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
      })

Python

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)

Clone this wiki locally