SOAP to REST
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:
- Upstream Target - https://www.dataaccess.com/webservicesserver/numberconversion.wso
- The WSDL definition from - https://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL
- Postman Client (or other endpoint testing tool)
Step 1: Import the WSDL API
- Select APIs from the System Management menu
- Click Import API
- Select From WSDL from the Import an API Definition window
- In the Upstream Target field, enter
https://www.dataaccess.com/webservicesserver/numberconversion.wso
as listed in the Prerequisites. - Paste the WSDL definition from the link in Prerequisites
- Click Generate API. You should now have an API named
NumberConversion
in your API list
Step 2: Add the transforms to an Endpoint
- From the API list, select Edit from the Actions menu for the
NumberConversion
API - Select the Endpoint Designer tab. You should see 2 POST endpoints that were imported. We will apply the transforms to the
NumberToWords
endpoint.
- 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.
- 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}}
.
- 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>
- In the Input field, enter the following:
{
"numberToConvert": 35
}
Note
The ‘35’ integer can be any number you want to convert
- 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}}
- For the Input Type, select XML
- In the Template section enter:
{
"convertedNumber": "{{.Envelope.Body.NumberToDollarsResponse.NumberToDollarsResult}}"
}
- 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>
- 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
- Expand the Modify Header plugin
- From the Request tab enter the following in the Add this header section
- Header Name:
content-type
- Header Value:
text/xml
- Click Add
- From the Response tab enter the following in the Add this header section
- Header Name:
content-type
- Header Value:
application/json
- Click Add
- Click Update
Testing the Endpoint
You now need to test the endpoint. We are going to use Postman.
Note
We have not setup any Authentication for this API, it has defaulted to Open (Keyless)
.
- Copy the URL for your NumberConversion API with the NumberToWords endpoint -
https://tyk-url/numberconversion/NumberToWords/
- Paste it as a POST URL in the Postman URL Request field
- Enter the following as a raw Body request
{
"numberToConvert": 35
}
Your Postman request should look similar to below (apart from the URL used)