I was working on a mule flow that submit a static JSON request to a REST endpoint. (The variable part was the id in the URL). My first attempt was to set the JSON request directly using <set-payload>.
<set-variable variableName="orderId" value="#[payload.id]" doc:name="set orderId"/> <set-payload value="{'note' : 'Order auto-approved by X', 'sendEmail' : true}" doc:name="Set Payload"/> <http:request config-ref="WS_CONFIG" path="/order/#[flowVars.orderId]/approve" method="POST" doc:name="REST approve request"> <http:request-builder> <http:header headerName="Content-Type" value="application/json"/> </http:request-builder> </http:request>
However, mule refused to submit this request, complaining about ‘Message payload is of type: String’. Most pages I found from googling suggested using the DataWeave Transformer. It can transform data to and from a large range of format, including flow variables into JSON. But the DataWeave Transformer was only available in the enterprise edition. After a frustrating hour of more googling and testing various different transformer, I found another way to achieve this easily by using a expression transformer:
<set-variable variableName="orderId" value="#[payload.id]" doc:name="set orderId"/> <expression-transformer expression="#[['note' : 'Order auto-approved by X', 'sendEmail' : true]]" doc:name="set payload"/> <json:object-to-json-transformer doc:name="Object to JSON"/> <http:request config-ref="WS_CONFIG" path="/order/#[flowVars.orderId]/approve" method="POST" doc:name="REST approve request"> <http:request-builder> <http:header headerName="Content-Type" value="application/json"/> </http:request-builder> </http:request>
The flow I worked on didn’t need the order id in the JSON request. But you can reference flow variables in the payload like this:
<set-variable variableName="orderId" value="#[payload.id]" doc:name="set orderId"/> <expression-transformer expression="#[['note' : 'Order auto-approved by X', 'id':flowVars.orderId, 'sendEmail' : true]]" doc:name="set payload"/>