When exposing a REST API, you can customize the information sent in responses.

For that, do the following:

  1. In the REST API, set property 'On Response' to 'New OnResponse';
  2. Open the ‘OnResponse’ callback (that now is available under the REST API);
  3. Implement the logic to customize the information of the response.

Example

In this example we have a Contacts application that exposes a REST API. We want to return all responses in XML format, instead of the default JSON.

Consider we have an action in the module to convert JSON into XML: ConvertJSON2XML

Consider we have an action in the module to convert JSON into XML: ConvertJSON2XML

In the exposed ContactsAPI REST API, go to property 'On Response' and select ‘New OnResponse’. This makes the callback available.

 

 

Implement the callback to return responses in XML format, instead of the default JSON:

  1. In the flow of the OnResponse callback, put action ConvertJSON2XML before the assign;
  2. In the OnResponse callback, put action ConvertJSONtoXML before the assign;
  3. In the assign, set the customized response with the result of the action:

CustomizedResponse.ResponseText = ConvertJSON2XML.Xml

  1. Add a reference to action AddHeader (from extension HttpRequestHandler), to set the response format to XML.
  2. Put action AddHeader after the assign passing the following values:

Name: "content-type"

Value: "application/xml"

Test Your Method

We've deployed the application to our environment in the public cloud.

To test the response in XML format, we use the following curl command:

curl https://osacademy.outsystemscloud.com/Contacts/rest/ContactsAPI/GetContact?ContactId=2

The response in XML is:

See also

Expose Data Using REST | Preprocess REST API Requests | Exposed REST API Method Properties