We need to generate a JSON output in case of fault. RaiseFault policy is appropriate for the same. Following is an example policy
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="invalid_login">
<FaultResponse>
<Set>
<Payload contentType="application/json" variablePrefix="%" variableSuffix="#">
{
"error": "Incorrect login credentials",
"source": "API error"
}
</Payload>
<StatusCode>400</StatusCode>
</Set>
</FaultResponse>
</RaiseFault>
Why this so different - please note the variablePrefix, variableSuffix being present even though the content type is application/json and no variables are used. contentType attribute does not have the same impact as in AssignMessage policy. The presence of {} leads to the interpretation of the data between them as variable name. The variablePrefix and variableSuffix change solves the problem.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="invalid_login">
<FaultResponse>
<Set>
<Payload contentType="application/json" variablePrefix="%" variableSuffix="#">
{
"error": "Incorrect login credentials",
"source": "API error"
}
</Payload>
<StatusCode>400</StatusCode>
</Set>
</FaultResponse>
</RaiseFault>
Why this so different - please note the variablePrefix, variableSuffix being present even though the content type is application/json and no variables are used. contentType attribute does not have the same impact as in AssignMessage policy. The presence of {} leads to the interpretation of the data between them as variable name. The variablePrefix and variableSuffix change solves the problem.