Tech

How to consume RPC-style web service in Mule 4

Photo of Alessandro Toscano, Solution Architect Written by Alessandro Toscano, Solution Architect,   Jul 1, 2019

Recently, one of our customers requested to consume RPC-style web services built on top of a legacy system. Unfortunately, I then discovered that RPC WSDL were not supported by the Web Service Consumer connector in Mule 4. Want to know the solution?

MuleSoft Connectors abstract and facilitate integrations with third-party APIs. In many cases, you can use an existing certified MuleSoft connector from Exchange, such as Workday or Salesforce, to connect to a service provider. However, when no connector is available for a specific Web service, the easiest way to consume the service from your Mule app is to use the Web Service Consumer connector but it's not always possible.

In our case, the approach was to build a SOAP message, defining the SOAP envelope, header and body elements as required and then use the standard HTTP Request Connector to POST the data.

post data request

Let's have a look at Mule 4 components used in our Mule application flow.

  • Parse Template - A transformer that parses a template defined inline
  • HTTP Request - To consume a HTTP service

From your project in MuleSoft's Anypoint Studio create a new XML file under resources and copy the required SOAP envelope into it. You can generate your SOAP request using a SOAP client, such as SoapUI.

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:api="http://www.myexample.com/api/">
   <soap:Header/>
   <soap:Body>
      <api:getStaffDetails>
         <api:acct_userid></api:acct_userid>
         <api:acct_password></api:acct_password>
         <api:employee_id></api:employee_id>
      </api:getStaffDetails>
   </soap:Body>
</soap:Envelope>

Now, we will use DataWeave to parameterise our request. Assuming we have stored our input parameters for your SOAP request in variables earlier in our flow, we will be referring to these variables in our template as shown below

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:api="http://www.myexample.com/api/">
   <soapenv:Header/>
   <soap:Body>
      <api:getStaffDetails>
         <api:acct_userid>#[vars.userid]</api:acct_userid>
         <api:acct_password>#[vars.pwd]</api:acct_password>
         <api:employee_id>#[vars.employeeId]</api:employee_id>
      </api:getStaffDetails>
   </soap:Body>
</soap:Envelope>  

The XML file containing our SOAP request will now be referenced in the Parse Template processor. The Parse Template is used for processing a template, in this case the XML request created above. It will evaluate all Mule expressions in our template, replace them with their result and store it into the payload.

In the component, under General, specify the location of the file; you can use the browse option to explore your project and select the correct XML template.

Mule - Parse Template

Next, we use the HTTP Request Connector to POST the payload to the Web Service endpoint.

Under General tab of the component specify POST as method and URL with the endpoint of the RPC web service to consume.

Mule - HTTP Request

Finally we’ll set the HTTP headers (not to be confused with headers in the SOAP envelope). Specify ContentType as text/xml as below. It may also be necessary to specify SOAPAction if it's required by the Web Service for the request to work.

Mule - HTTP Request (headers)

We are now ready to run our Mule project and consume our RPC-style web services!

I hope you found this tip useful. Let me know if you have any comments or questions.

 

Topics: MuleSoft Tech

We’d love to hear your opinion on this post