18
Parameters as a part of Message body By Anirban Sen Chowdhary

Parameters as a part of body

Embed Size (px)

Citation preview

Page 1: Parameters as a part of body

Parameters as a part of Message body

By Anirban Sen Chowdhary

Page 2: Parameters as a part of body

Today we will be looking at how to map an inbound properties of a Mule message with the Message payload

Page 3: Parameters as a part of body

But before we go forward let’s quickly into what is a Mule Messages and the structure of a Mule message

Page 4: Parameters as a part of body

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 param2) Payload :- Contains the actual message content3) Attachment :- It’s optional field may or may not contain

attachment

Image source :- Mule Soft

Mule Message

Page 5: Parameters as a part of body

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

Page 6: Parameters as a part of body

But how can we extract the value of inbounds properties in our application??

.

Page 7: Parameters as a part of body

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 :-

Page 8: Parameters as a part of body

Now what if we want to put the query parameter as a part of Message payload and not as an Inbound properties of Message ???

Page 9: Parameters as a part of body

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

Page 10: Parameters as a part of body

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

Page 11: Parameters as a part of body

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

Page 12: Parameters as a part of body

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>

Page 13: Parameters as a part of body

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]

Page 14: Parameters as a part of body

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

Page 15: Parameters as a part of body

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

Page 16: Parameters as a part of body

For more information on this http:body-to-parameter-map-transformer please visit MuleSoft documentation :- http://www.mulesoft.org/documentation/display/current/HTTP+Transport+Reference

PS: As of Mule 3.6 and later, the HTTP endpoint-based connector and transport has been replaced by an operation-based HTTP connector.

Page 17: Parameters as a part of body

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

Page 18: Parameters as a part of body

Thank You