By Anirban Sen Chowdhary
Today we will be looking at how to map an inbound properties of a
Mule message with the Message payload
But before we go forward let’s quickly into what is a Mule Messages and
the structure of a Mule message
If we look at the Mule message format, we find that Mule message is
generally divided into 3 parts as follows:-
You can see there are basically 3 parts :-
1) Properties :- Outbound or Inbound properties like our query param
2) Payload :- Contains the actual message content
3) Attachment :- It’s optional field may or may not contain attachment
Image source :- Mule Soft
Now, in our application what ever we send as a query parameter in the
browser comes into our application as inbound properties of Message.
For example if we use the following url in our browser :-
http://localhost:8888/test?id=231&name=Anirban
Here we are sending 2 query parameters id=231&name=Anirban in our
application which will come as an inbound properties in our application
.
We can use following MEL expression in our logger to extract the values of
query parameter in our Mule application :-
#[message.inboundProperties['id']] #[message.inboundProperties['name']]
If we put the above expression in a logger :-
<logger message="inbound #[message.inboundProperties['id']]
#[message.inboundProperties['name']]" level="INFO" doc:name="Logger"/>
we will get the following values in our console :-
Now what if we want to put the query parameter as a part of
Message payload and not as an Inbound properties of Message ???
Yes, you can easily map an Inbound properties of a Mule Message
with Mule payload by using :-
<http:body-to-parameter-map-transformer/>
This component will transform all the Inbound properties ( query
parameter in our case ) as a part of actual Message payload
Now, we will see a simple example to know how the component
http:body-to-parameter-map-transformer transform all the
Inbound message properties into actual message payload
You can see a simple flow, where we have put http:body-to-parameter-
map-transformer after Http inbound endpoint. The main task of this
component will be transforming all the Inbound properties of messages
into the message payload as a hash map of name-value pairs
The flow in the Mule config will be :-
<flow name="Body-To-Parameter-MapFlow1" doc:name="Body-To-Parameter-MapFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8888" path="test"
doc:name="HTTP" />
<logger message="inbound #[message.inboundProperties['id']] #[message.inboundProperties['name']]" level="INFO"
doc:name="Logger"/>
<http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>
<logger level="INFO" category="Log1" doc:name="Logger" message="#[message.payload['id']]
#[message.payload['name']]"/>
<logger level="INFO" category="Full Message" doc:name="Logger" message="#[message.payload]"/>
</flow>
The flow in the Mule config will be :-
<flow name="Body-To-Parameter-MapFlow1" doc:name="Body-To-Parameter-MapFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8888" path="test"
doc:name="HTTP" />
<logger message="inbound #[message.inboundProperties['id']] #[message.inboundProperties['name']]" level="INFO"
doc:name="Logger"/>
<http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>
<logger level="INFO" category="Log1" doc:name="Logger" message="#[message.payload['id']]
#[message.payload['name']]"/>
<logger level="INFO" category="Full Message" doc:name="Logger" message="#[message.payload]"/>
</flow>
As you can see here we have put 2 loggers to check the message payload
content after http:body-to-parameter-map-transformer with the MEL
expression :- #[message.payload['id']] and #[message.payload['name']]
And to check full message payload :- #[message.payload]
If we check behind the scene what the http:body-to-parameter-map-
transformer is actually doing :-
You can see it’s transforming message properties into payload content
Now, if we test our application and hit the following url :-
http://localhost:8888/test?id=231&name=Anirban
We will get the following in our console :-
You can see in the log that the query parameters are now part of actual
message payload
For more information on this http:body-to-parameter-map-transformer
please visit MuleSoft documentation :-
http://www.mulesoft.org/documentation/display/current/HTTP+Transport+R
eference
PS: As of Mule 3.6 and later, the HTTP endpoint-based connector and transport has been
replaced by an operation-based HTTP connector.
Hope you enjoyed the simple yet an amazing trick in Mule.
And I wish these simple trick would be beneficial for a beginner in
Mule and help him/her in learning and implementing these concepts in
Mule
Parameters as a part of body

Parameters as a part of body

  • 1.
    By Anirban SenChowdhary
  • 2.
    Today we willbe looking at how to map an inbound properties of a Mule message with the Message payload
  • 3.
    But before wego forward let’s quickly into what is a Mule Messages and the structure of a Mule message
  • 4.
    If we lookat the Mule message format, we find that Mule message is generally divided into 3 parts as follows:- You can see there are basically 3 parts :- 1) Properties :- Outbound or Inbound properties like our query param 2) Payload :- Contains the actual message content 3) Attachment :- It’s optional field may or may not contain attachment Image source :- Mule Soft
  • 5.
    Now, in ourapplication what ever we send as a query parameter in the browser comes into our application as inbound properties of Message. For example if we use the following url in our browser :- http://localhost:8888/test?id=231&name=Anirban Here we are sending 2 query parameters id=231&name=Anirban in our application which will come as an inbound properties in our application
  • 6.
  • 7.
    We can usefollowing MEL expression in our logger to extract the values of query parameter in our Mule application :- #[message.inboundProperties['id']] #[message.inboundProperties['name']] If we put the above expression in a logger :- <logger message="inbound #[message.inboundProperties['id']] #[message.inboundProperties['name']]" level="INFO" doc:name="Logger"/> we will get the following values in our console :-
  • 8.
    Now what ifwe want to put the query parameter as a part of Message payload and not as an Inbound properties of Message ???
  • 9.
    Yes, you caneasily map an Inbound properties of a Mule Message with Mule payload by using :- <http:body-to-parameter-map-transformer/> This component will transform all the Inbound properties ( query parameter in our case ) as a part of actual Message payload
  • 10.
    Now, we willsee a simple example to know how the component http:body-to-parameter-map-transformer transform all the Inbound message properties into actual message payload
  • 11.
    You can seea simple flow, where we have put http:body-to-parameter- map-transformer after Http inbound endpoint. The main task of this component will be transforming all the Inbound properties of messages into the message payload as a hash map of name-value pairs
  • 12.
    The flow inthe Mule config will be :- <flow name="Body-To-Parameter-MapFlow1" doc:name="Body-To-Parameter-MapFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8888" path="test" doc:name="HTTP" /> <logger message="inbound #[message.inboundProperties['id']] #[message.inboundProperties['name']]" level="INFO" doc:name="Logger"/> <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/> <logger level="INFO" category="Log1" doc:name="Logger" message="#[message.payload['id']] #[message.payload['name']]"/> <logger level="INFO" category="Full Message" doc:name="Logger" message="#[message.payload]"/> </flow>
  • 13.
    The flow inthe Mule config will be :- <flow name="Body-To-Parameter-MapFlow1" doc:name="Body-To-Parameter-MapFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8888" path="test" doc:name="HTTP" /> <logger message="inbound #[message.inboundProperties['id']] #[message.inboundProperties['name']]" level="INFO" doc:name="Logger"/> <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/> <logger level="INFO" category="Log1" doc:name="Logger" message="#[message.payload['id']] #[message.payload['name']]"/> <logger level="INFO" category="Full Message" doc:name="Logger" message="#[message.payload]"/> </flow> As you can see here we have put 2 loggers to check the message payload content after http:body-to-parameter-map-transformer with the MEL expression :- #[message.payload['id']] and #[message.payload['name']] And to check full message payload :- #[message.payload]
  • 14.
    If we checkbehind the scene what the http:body-to-parameter-map- transformer is actually doing :- You can see it’s transforming message properties into payload content
  • 15.
    Now, if wetest our application and hit the following url :- http://localhost:8888/test?id=231&name=Anirban We will get the following in our console :- You can see in the log that the query parameters are now part of actual message payload
  • 16.
    For more informationon this http:body-to-parameter-map-transformer please visit MuleSoft documentation :- http://www.mulesoft.org/documentation/display/current/HTTP+Transport+R eference PS: As of Mule 3.6 and later, the HTTP endpoint-based connector and transport has been replaced by an operation-based HTTP connector.
  • 17.
    Hope you enjoyedthe simple yet an amazing trick in Mule. And I wish these simple trick would be beneficial for a beginner in Mule and help him/her in learning and implementing these concepts in Mule