Log in to the API Publisher and click the
PhoneVerification
API.
Click the Edit link next to the API's name to go to its edit mode.
Add the following resource to the API.
Tip: The resource you create here invokes the SOAP 1.2 Web service of the backend. Therefore, the recommended method is HTTP POST. As you do not include the payload in a query string, avoid giving any specific name in the URL pattern, which will be amended to the actual backend URL.
Field Sample value Resources URL pattern /* Request types POST
After the resource is added, expand it and add a parameter as follows. This parameter is used to pass the payload to the backend.
Parameter name Description Parameter Type Data Type body Pass the phone number and license key body String Next, let's write a sequence to convert the JSON payload to a SOAP request. We do this because the backend accepts SOAP requests.
Navigate to the Implement page and change the endpoint of the API to http://ws.cdyne.com/phoneverify/phoneverify.asmx?WSDL. Once the edits are done, click Save.
Download WSO2 Developer Studio (version 3.7.1 is used here) from http://wso2.com/products/developer-studio/ and open it by double clicking the
Eclipse.app
file inside the downloaded folder.Tip: To start Eclipse on a Mac for the first time, open a terminal and execute the following commands:
cd <DevStudio_Home>/Eclipse.app/Contents/MacOS/
chmod +x eclipse
./eclipse
Thereafter, you can start Eclipse by double-clicking the Eclipse icon in
<DevStudio_Home>
.- Click the Developer Studio menu and choose Open Dashboard. When the dashboard opens, click the ESB Config Project link.
- Create a new ESB project by the name
PhoneProject
.
Click the Sequence link on the Developer Studio Dashboard and create a new sequence by the name
JSONtoSOAP
in thePhoneProject
.
Your sequence now appears on the Developer Studio console. From under the Mediators section, drag and drop a PayloadFactory mediator to your sequence and give the following values to the mediator.
Tip: The PayloadFactory mediator transforms the content of your message. The
<args>
elements define arguments that retrieve values at runtime by evaluating the provided expression against the SOAP body. You can configure the format of the request/response and map it to the arguments.For example, in the following configuration, the values for the format parameters
PhoneNumber
andLicenseKey
will be assigned with values that are taken from the <args> elements (arguments,) in that particular order.For details on how you got this configuration, see PayloadFactory Mediator in the WSO2 ESB documentation.
Payload <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <CheckPhoneNumber xmlns="http://ws.cdyne.com/PhoneVerify/query"> <PhoneNumber>$1</PhoneNumber> <LicenseKey>$2</LicenseKey> </CheckPhoneNumber> </soap12:Body> </soap12:Envelope>
Args Give the following arguments:
Type Value Evaluator expression //request/PhoneNumber
xml expression //request/LicenseKey
xml Similarly, add a property mediator to the same sequence and give the following values to the property mediator.
Property Name messageType Value Type Literal Value application/soap+xml Property Scope axis2 Save the sequence, which is in XML format (e.g.,
JSONtoSOAP.xml
). This will be theIn
sequence for your API.Go back to the Developer Studio Dashboard, click the Sequence link and create another sequence by the name
SOAPtoJSON
in thePhoneProject
.
Add a property mediator to the second sequence and give the following values to the property mediator.
Property Name messageType Value Type Literal Value application/json Property Scope axis2 Save the sequence, which is in XML format (e.g.,
SOAPtoJSON.xml
). This will be theOut
sequence for your API.- Log in to the API Gateway's management console. If you are using WSO2 Cloud, the Gateway URL is
https://gateway.api.cloud.wso2.com/carbon/admin/login.jsp
. If you are using a local setup, the URL ishttps://localhost:9443/carbon
. You can see the username on the top right-hand corner of the API Publisher. After logging in, click the Browse menu under the Resources menu.
- When the API Gateway's registry opens, navigate to the registry path
/_system/governance/apimgt/customsequences/in
. This is because you want the custom sequence to be invoked in theIn
direction or the request path.
Click Add Resource and upload the XML file of the sequence that you created earlier.
Next, let's write another sequence to convert the SOAP response that the backend sends to JSON.- Similarly, navigate to the registry path
/_system/governance/apimgt/customsequences/out
and upload theSOAPtoJSON.xml
sequence file. This will invoke the second sequence in theOut
direction or the response path.
- Log back to the API Publisher, click the Edit link associated wit the API, navigate to the Manage tab, click the Sequences check-box and engage the
In
andout
sequences that you created earlier.
- Save the API.
You have created an API, a resource to access the SOAP backend and engaged sequences to the request and response paths to convert the message format from JSON to SOAP and back to JSON. Let's subscribe to the API and invoke it. Log in to the API Store and subscribe to the API and create an access token if you have not done so already.
Go to the API Console tab and expand the POST method.
Give the payload in the
body
parameter in JSON format and click Try it out. Here's a sample JSON payload: {"request":{"PhoneNumber":"18006785432","LicenseKey":"0"}}
Note that you get a JSON response to the JSON request whereas the backend accepts SOAP messages. The request and response are conve rted by the sequences that you engaged at the API Gateway.
In this tutorial, you converted a message from JSON to SOAP and back to JSON using In
and Out
sequences.