Reputation: 1187
I want to set to http.status code in Dataweave transformation. For this I am setting http.status
on Outbound Property
tab using below code:
Outbound Property ===> http.status
%dw 1.0
%output application/java
---
"404" when payload[0] == null
otherwise "200"
But it is not reflected even when payload is null. Can any suggest?
EDIT:
For reference here is the full Dataweave code
<dw:transform-message doc:name="buildResponse">
<dw:set-payload>
<![CDATA[%dw 1.0
%input payload application/java
%output application/json
---
{
"customer": {
"id": payload[0].ID,
"name": payload[0].NAME,
"age": payload[0].AGE,
"address": {
"line1": payload[0].LINE1,
"line2": payload[0].LINE2,
"city": payload[0].CITY,
"state": payload[0].STATE,
"pincode": payload[0].PINCODE
}
}
} when (sizeOf payload) > 0
otherwise
{
"customer" : "not found"
}]]>
</dw:set-payload>
<dw:set-property propertyName="http.status">
<![CDATA[%dw 1.0
%output application/java
---
"404" when (sizeOf payload) == 0
otherwise "200"]]>
</dw:set-property>
</dw:transform-message>
Upvotes: 0
Views: 3243
Reputation: 1187
Ryan Carter's answer is correct. However, there is a specific answer without storing the original payload into a flow variable.
<dw:transform-message doc:name="buildResponse">
<dw:set-payload>
<![CDATA[%dw 1.0
%input payload application/java
%output application/json
---
{
"customer": {
"id": payload[0].ID,
"name": payload[0].NAME,
"age": payload[0].AGE,
"address": {
"line1": payload[0].LINE1,
"line2": payload[0].LINE2,
"city": payload[0].CITY,
"state": payload[0].STATE,
"pincode": payload[0].PINCODE
}
}
} when (sizeOf payload) > 0
otherwise
{
"customer" : "not found"
}]]>
</dw:set-payload>
<dw:set-property propertyName="http.status">
<![CDATA[%dw 1.0
%output application/java
---
"404" when payload.customer == "not found"
otherwise "200"]]>
</dw:set-property>
</dw:transform-message>
I did like this: "404" when payload.customer == "not found"
Upvotes: 0
Reputation: 11606
EDIT.
This is because the first part of the dataweave script is overwriting the payload to the JSON. So when you use sizeOf in the set-property dw script it's actually doing a sizeOf the JSON string which is not 0.
To make this work, I would probably store the original payload in a flowVar and use that in the dw script like so:
<http:listener-config name="HTTP_Listener_Configuration"
host="0.0.0.0" port="8085" doc:name="HTTP Listener Configuration" />
<flow name="dataweave-testFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/test" doc:name="HTTP" />
<set-payload value="#[[]]" doc:name="Set Payload" />
<set-variable variableName="originalPayload" value="#[payload]"
doc:name="Variable" />
<dw:transform-message doc:name="buildResponse">
<dw:set-payload>
<![CDATA[%dw 1.0
%input payload application/java
%output application/json
---
{
"customer": {
"id": payload[0].ID,
"name": payload[0].NAME,
"age": payload[0].AGE,
"address": {
"line1": payload[0].LINE1,
"line2": payload[0].LINE2,
"city": payload[0].CITY,
"state": payload[0].STATE,
"pincode": payload[0].PINCODE
}
}
} when (sizeOf payload) > 0
otherwise
{
"customer" : "not found"
}]]>
</dw:set-payload>
<dw:set-property propertyName="http.status">
<![CDATA[%dw 1.0
%output application/java
---
"404" when (sizeOf flowVars.originalPayload) == 0
otherwise "200"]]>
</dw:set-property>
</dw:transform-message>
</flow>
This is a bug with Dataweave in my opinion and created it here: https://www.mulesoft.org/jira/browse/MULE-9021
Upvotes: 1