Skip to content

The callback response

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

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.

Accept application/json

The response will be converted to JSON. Conversion only succeed if a Map/Object/Dictionary is returned from the Groovy/Javascript/Python closure.

Given the following client call

$ curl -H 'Accept application/json' http://localhost/rest/albums

and this Groovy show:

    rest.get('/albums').then { req ->
        [ [ name: 'La folie', band: 'The Stranglers'],
          [ name: 'Movement', band: 'New Order']]
    }

The HTTP response body will be

    [{"name":"La folie","band":"The Stranglers"},{"name":"Movement","band":"New Order"}]

Accept application/xml

The response will be converted to XML. The returned object must have one of the following types:

Given the following client call

$ curl -H 'Accept application/xml' http://localhost/rest/albums

and this Javascript show:

    rest.get('/albums').then(function (request) { 
         return { album: [
              { name: 'La folie', band: 'The Stranglers'},
              { name: 'Movement', band: 'New Order'}
          ]}
    })

The HTTP response body will be

<albums>
  <album>
    <name>La folie</name>
    <band>The Stranglers</band>
  </album>
  <album>
    <name>Movement</name>
    <band>New Order</band>
  </album>
</albums>

JAXB2 object

The same client call and the following Groovy show

@XmlRootElement
@XmlAccessorType( XmlAccessType.FIELD )
class Album {
  String band
  String name
}

rest.get('/albums').then {
  new Album(band:'Depenche Mode',name:'Violator')
}

will output the following HTTP response body

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<album>
  <band>Depenche Mode</band>
  <name>Violator</name>
</album>

A plain String value

The same client call and the following Python show

def handleDomXml(req):
        document = getDOMImplementation().createDocument(EMPTY_NAMESPACE,'Album',None)
        name = document.createElement('name')
        name.appendChild(document.createTextNode('Pornography'))
        band = document.createElement('band')
        band.appendChild(document.createTextNode('The cure'))
        bband = band.firstChild.nodeValue
        document.documentElement.appendChild(name)
        document.documentElement.appendChild(band)

        return document.toxml()

rest.get('/albums').then(handleDomXml)

will return this HTTP response body

<?xml version="1.0" ?>
<Album><name>Pornography</name><band>The cure</band></Album>

E4X XML

And finally the following Javascript code snippet

rest.get('/albums').then(function(request) {
  var album = "La folie"
  var band = "The Stranglers"

  return <albums><album name={album} band={band}/></albums>
})

will return the following response

<albums>
  <album band="The Stranglers" name="La folie"/>
</albums>

Clone this wiki locally