Transformation Use Case: SOAP To REST

Last updated: 3 minutes read.

Introduction

You can transform an existing SOAP service to a JSON REST service. This can be done from the Tyk Dashboard with no coding involved and should take around 10 minutes to perform the transform.

We also have a video which walks you through the SOAP to REST transform.

Prerequisites

An existing SOAP service and the WSDL definition. For this example, we will use:

Step 1: Import the WSDL API

  1. Select APIs from the System Management menu

APIs Menu

  1. Click Import API

Import API

  1. Select From WSDL from the Import an API Definition window
  2. In the Upstream Target field, enter https://www.dataaccess.com/webservicesserver/numberconversion.wso as listed in the Prerequisites.
  3. Paste the WSDL definition from the link in Prerequisites
  4. Click Generate API. You should now have an API named NumberConversion in your API list

NumberService API

Step 2: Add the transforms to an Endpoint

  1. From the API list, select Edit from the Actions menu for the NumberConversion API
  2. Select the Endpoint Designer tab. You should see 2 POST endpoints that were imported. We will apply the transforms to the NumberToWords endpoint.

Endpoints

  1. Expand the NumberToWords endpoint. The following plugins should have been added as part of the import process.
  • URL rewrite
  • Track endpoint

Note

To make the URL a little friendlier, we’re going to amend the Relative Path to just /NumberToWords. Update your API after doing this.

4. Add the following plugins from the Plugins drop-down list:

  • Body transform
  • Modify headers

Step 3: Modify the Body Transform Plugin

Set up the Request

We use the {{.FieldName}} Golang template syntax to access the JSON request. For this template we will use {{.numberToConvert}}.

  1. Expand the Body transform plugin. From the Request tab, copy the following into the Template section:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.dataaccess.com/webservicesserver/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:NumberToDollars>
         <web:dNum>{{.numberToConvert}}</web:dNum>
      </web:NumberToDollars>
   </soapenv:Body>
</soapenv:Envelope>
  1. In the Input field, enter the following:
{
    "numberToConvert": 35
}

Note

The ‘35’ integer can be any number you want to convert

  1. Click Test. You should get the following in the Output field:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.dataaccess.com/webservicesserver/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:NumberToDollars>
         <web:dNum>35</web:dNum>
      </web:NumberToDollars>
   </soapenv:Body>
</soapenv:Envelope>

Set up the Response

Again, for the response, we will be using the {{.FieldName}} syntax as the following {{.Envelope.Body.NumberToDollarsResponse.NumberToDollarsResult}}

  1. For the Input Type, select XML

Response Input Type

  1. In the Template section enter:
{
    "convertedNumber": "{{.Envelope.Body.NumberToDollarsResponse.NumberToDollarsResult}}"
}
  1. Enter the following into the input field:
<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <NumberToDollarsResponse xmlns="http://www.dataaccess.com/webservicesserver/">
      <NumberToDollarsResult>thirty five dollars</NumberToDollarsResult>
    </NumberToDollarsResponse>
  </soap12:Body>
</soap12:Envelope>
  1. Click Test. You should get the following in the Output field:
{
    "convertedNumber": "thirty five dollars"
}

Step 5: Change the Content-Type Header

We now need to change the content-type header to allow the SOAP service to receive the payload in XML. We do this by using the Modify header plugin

  1. Expand the Modify Header plugin
  2. From the Request tab enter the following in the Add this header section
  • Header Name: content-type
  • Header Value: text/xml
  1. Click Add

Modify Header Request

  1. From the Response tab enter the following in the Add this header section
  • Header Name: content-type
  • Header Value: application/json

Modify Header Response

  1. Click Add
  2. Click Update

Update API

Testing the Endpoint

You now need to test the endpoint. We are going to use Postman.

Note

We have not set up any Authentication for this API, it has defaulted to Open (Keyless).

  1. Copy the URL for your NumberConversion API with the NumberToWords endpoint - https://tyk-url/numberconversion/NumberToWords/
  2. Paste it as a POST URL in the Postman URL Request field
  3. Enter the following as a raw Body request
{
    "numberToConvert": 35
}

Your Postman request should look similar to below (apart from the URL used)

Postman