Unlock the Power of Mule Flows: Learn How to Send SOAP and Transport Headers Like a Pro
As a seasoned MuleSoft expert, I recently led API implementations for a prominent client using MuleSoft’s CloudHub. One common requirement that emerged was the need to integrate an external web service into Mule flows. The MuleSoft HTTP Request Connector and Web Service Consumer are the go-to connectors for developers seeking to tap into external web services from Mule flows. When making HTTP requests to an external web service, these connectors require configuration with all requisite parameters, including endpoint URL, HTTP method/operations, headers, and authentications.
In this article, I will explore the process of sending headers (SOAP headers and transport headers) when invoking external web services from Mule flows.
Sending SOAP Headers
When consuming external SOAP services from a Mule flow, we can transmit SOAP headers to the external web service by leveraging the MuleSoft property transformer. To do this, we need to establish outbound properties with the prefix “soap.” using the MuleSoft property transformer. Outbound properties that commence with a “soap.” prefix will be treated as SOAP headers and disregarded by the transport. Conversely, all properties that aren’t named with a “soap.” prefix will be treated as HTTP transport headers (by default, the WSC employs the HTTP transport).
For example, we can use the MuleSoft property transformer to create a SOAP header named “cccmoiheaders.” The value of these headers can be static or dynamic (by reading from flowVars or property placeholders).
To learn more about sending SOAP and transport headers in Mule flows, check out this in-depth guide: https://t8tech.com/it/architecture/unlock-the-power-of-mule-flows-learn-how-to-send-soap-and-transport-headers-like-a-pro/
<set-property propertyName="soap.cccmoiheaders" value="<wsse:Security
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/&quot;
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd&quot;
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot; soapenv:mustUnderstand="1"><
wsse:UsernameToken wsu:Id="UsernameToken-03184DA938DBE5406314344062579892"><wsse:Username>${BW.UserName}</wsse:Username><
wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText&quot;&gt;${BW.Password}&lt;/wsse:Password&gt;&lt;
/wsse:UsernameToken></wsse:Security>"
doc:name="BW_Headers"/>
The output of this code snippet will generate a SOAP header named “cccmoiheaders” with the value below.
<wsse:Security
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
<wsse:UsernameToken wsu:Id="UsernameToken-03184DA938DBE5406314344062579892">
<wsse:Username>abcddddd</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">absbbsbs</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
2. Customizing Transport Headers: When integrating Mule flows with external web services (REST via HTTP Connector or SOAP via Web Service Consumer), various options are available for sending transport headers. Let’s delve into the process of transmitting transport-level headers from Mule flows when utilizing these connectors to invoke external web services.
2.1 Utilizing the Web Service Consumer: To transmit transport-level headers while consuming an external SOAP service using the WS Consumer, MuleSoft’s property transformer is the optimal solution. Here, we need to establish outbound properties (excluding the “soap.” prefix) using the MuleSoft property transformer prior to the WS Consumer. These outbound properties will be treated as transport headers (by default, the WSC employs the HTTP transport).
The following code snippet will create a transport header named “Content-Type” with the value “application/json”:
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
2.2 Harnessing the Power of the HTTP Request Connector: When integrating external web services via the HTTP Request Connector, you can transmit transport-level headers by leveraging the MuleSoft property transformer (as outlined in section 2.1) or by explicitly defining the transport headers within the HTTP Request Connector’s configuration settings. Outbound properties present in the Mule message that reaches the HTTP Request Connector are automatically appended as HTTP request headers. The screenshot below illustrates the header configuration in the HTTP Requester Configuration, where the header values are static, but can be dynamically retrieved from flowVars or a property placeholder.
The approaches outlined above play a crucial role in sending headers when invoking external web services from a Mule flow. Furthermore, in Mule flows, you can replicate all existing properties from the inbound scope to the outbound scope of the message using the “copy-properties” feature. Please refer to the code snippet below for configuring copy-properties in Mule.
<copy-properties propertyName="http.*" doc:name="Copy All HTTP Headers"/>
Let’s share our expertise to enrich our MuleSoft community.
Thank you!
Source link
lol