The <rest:response/> element

A <rest:response/> node may be returned from a function either with or without the resource body content. The purpose of the <rest:response/> node is to control the HTTP response sent back to the client of the RESTful web service.

Syntax of Response Format

<rest:response>
  (http:response?)
</rest:response>

<http:response status?="integer" message?="string">
  (http:header*)
</http:response>

<http:header name="string" value="string"/>

Should the status be omitted for the response, or should a REST Response document not be returned from a function, then the status defaults to 200 OK.

As an example, the following response can be returned to trigger a client-side redirection

declare namespace http = "http://expath.org/ns/http-client";

declare
  %rest:path("/temp-redirect")
function redirect() {
  <rest:response>
    <http:response status="302" message="Temporary Redirect">
      <http:header name="location" value="/new/location" />
    </http:response>
  </rest:response>
};

The following example mixes controlling the HTTP response status and headers with some content being given as the resource

declare namespace http = "http://expath.org/ns/http-client";

declare
  %rest:path("/error-500")
function error-500() {
  <rest:response>
    <http:response status="500" message="Internal Server Error">
      <http:header name="X-Status" value="System not Operational" />
      <http:header name="X-Server-Version" value="Version 3.5" />
      <http:header name="Content-Type" value="text/xml" />
    </http:response>
  </rest:response>,
  <html>
    <head>
      <title>500 Error occurred</title>
    </head>
    <body>500 Internal Server Error</body>
  </html>
};
TOP