DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Working With Headers in Mule Flows

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.

Rakesh Kumar Jha user avatar by
Rakesh Kumar Jha
·
Oct. 05, 17 · Integration Zone · Tutorial
Like (8)
Save
Tweet
36.07K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Flow (web browser) Web Service Property (programming) MuleSoft Connector (mathematics) SOAP Web Protocols

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Perform Visual Regression Testing Using Cypress
  • How to Handle Early Startup Technical Debt (Or Just Avoid it Entirely)
  • 6 Best Books to Learn Multithreading and Concurrency in Java
  • Why to Implement GitOps into Your Kubernetes CI/CD Pipelines

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo