3

I'm trying to add some additional static data to an inbound http message (received as URL parameters) payload before submitting it to an outbound http form based endpoint. My mule config is as follows :

<flow name="login" doc:name="login">
    <http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:8081/login" doc:name="Login"/>

    <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>  

    <http:outbound-endpoint address="http://localhost:8090/mayapp/Main/login.do"
            method="POST" contentType="application/x-www-form-urlencoded" exchange-pattern="request-response">
    </http:outbound-endpoint>
</flow>

The above transforms the URL parameters to a http form POST (name/values pairs) very nicely. What I need now is the ability to add new name-value pairs to the POST(ed) data ? The form I'm posting to expects some static data (posted as hidden HTML fields) that I would like to handle as part of the transformation process.

I've managed to accomplish this using a custom component. I'm wondering if there's an easier way to handle this using Mule's native transformers / message processors !

groovenarula
  • 95
  • 2
  • 2
  • 6

2 Answers2

5

First I would use a transformer and not a component for this, as it is really a transformation you're doing on the payload data.

Second I can't think of another transformer than the Groovy one to modify the Map payload created by the body-to-parameter-map-transformer. Something like:

<script:transformer>
    <script:script engine="groovy">
        <script:text>
            payload['newKey'] = 'newValue'
        </script:text>
    </script:script>
</script:transformer>
David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • How about writing your own AbstractMessageTransformer? That one can access the http request parameters and modify your payload (or do whatever...) – Dirk Schumacher Jan 24 '14 at 09:54
  • Sure, if you prefer compiling your extensions. Otherwise, scripted transformers, or since Mule 3.3, MEL-based expression transformers can both access the HTTP request parameters and modify the payload too. – David Dossot Jan 24 '14 at 16:24
0

You can use an expression-component as well:

<expression-component doc:name="change_payload">
   <![CDATA[#[ message.payload = 'foo';]]]>
</expression-component>
Rondo
  • 3,458
  • 28
  • 26