Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Working With Headers in Mule Flows

DZone's Guide to

Working With Headers in Mule Flows

Learn how to send SOAP and transport headers while invoking external web services from Mule flows, as for API implementations, in this tutorial.

Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90 day trial 3Scale by Red Hat

As a MuleSoft Certified Architect, Designer and Developer, I recently worked on API implementations for one of our clients using MuleSoft’s CloudHub. One common feature that we needed to use across APIs implementations was to consume an external web service from Mule flows. MuleSoft HTTP Request Connector and Web Service Consumer are the most popular connectors which developers use to consume external web services from Mule flows. While making HTTP requests to an external web service, connectors need to be configured with all the required parameters like endpoint URL, HTTP method/operations, headers, and authentications.

In this blog, I will demonstrate how to send headers (SOAP headers and transport headers) while invoking external web services from Mule flows.

1. SOAP Headers: While consuming external SOAP services from a Mule flow, we can send SOAP headers to the external web service by using the MuleSoft property transformer.

Here, we need to create outbound properties with the prefix “soap.” using the MuleSoft property transformer. Outbound properties that begin with a “soap.” prefix will be treated as SOAP headers and ignored by the transport. All properties that aren’t named with a “soap.” prefix will be treated as HTTP transport headers (by default, the WSC uses the HTTP transport).

As we can see in the below screen, the MuleSoft property transformer named “BW_Headers” is used to create a SOAP header named “cccmoiheaders.”

Image title

The value of these headers can be static or can also be dynamic (by reading from flowVars or property placeholders). The above screen will generate the below code snippet:

<set-property propertyName="soap.cccmoiheaders" value="&lt;wsse:Security
xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;
xmlns:wsu=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd&quot;
xmlns:wsse=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot; soapenv:mustUnderstand=&quot;1&quot;&gt;&lt;
    wsse:UsernameToken wsu:Id=&quot;UsernameToken-03184DA938DBE5406314344062579892&quot;&gt;&lt;wsse:Username&gt;${BW.UserName}&lt;/wsse:Username&gt;&lt;
    wsse:Password Type=&quot;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&gt;&lt;/wsse:Security&gt;" 
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. Transport Headers: While consuming external web services (REST using HTTP Connector or SOAP using Web Service Consumer) from Mule flows, we can send transport headers in different ways.  Let’s see how we can send transport level headers from Mule flows while using these connectors to invoke external web services.

 2.1 Using Web Service Consumer: To send transport level headers while consuming an external SOAP service using WS Consumer, MuleSoft's property transformer is the best option. Here we need to create outbound properties (without the “soap.” prefix) using the MuleSoft property transformer before the WS Consumer. These outbound properties will be treated as transport headers (by default, the WSC uses the HTTP transport).

The below 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 Using HTTP Request Connector: To send transport level headers while consuming external web service using the HTTP Request Connector, we can use the MuleSoft property transformer (as described in section 2.1) or we can explicitly configure the transport headers in the HTTP Request Connector’s configuration. Outbound properties in the Mule message that arrives at the HTTP Request Connector are automatically added as HTTP request headers.

The below screen shows the header configuration in the HTTP Requester Configuration. Here the values of headers are static, but this can be fetched dynamically from flowVars or a property placeholder.

Image title

That concludes the approaches described above are useful to send headers while invoking external web services from a Mule flow. In Mule flows, we can also copy all the existing properties from the inbound scope onto the outbound scope of the message using “copy-properties.” Please see the below code snippet for how to configure copy-properties in Mule.

<copy-properties propertyName="http.*" doc:name="Copy All HTTP Headers"/>

Let’s share our knowledge to expand our MuleSoft community.

Thank you!

Discover how you can achielve enterpriese agility with microservices and API management

Topics:
mulesoft ,web service ,mule flow ,mule esb ,http ,soap ,integration ,headers

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}