Mule-Setting Status code in HTTP response
<p>I need to set Status code and Reason Phrase in mule HTTP using Mule Choice exception strategy under that is Catch exception strategy. Following the Mulesoft documentation <a href="https://docs.mulesoft.com/mule-user-guide/v/3.7/http-listener-connector#http-response-status-code-and-reason-phrase" rel="nofollow">https://docs.mulesoft.com/mule-user-guide/v/3.7/http-listener-connector#http-response-status-code-and-reason-phrase</a></p>

<p>I tried doing something like this : </p>

<pre><code>  &lt;choice-exception-strategy name="MyExptn"&gt;
&lt;rollback-exception-strategy when="#[exception.causedBy(org.mule.component.ComponentException)]" doc:name="ComponentException Strategy"&gt;

        &lt;set-variable variableName="errorTableNotPresent" value="400" doc:name="Set status code"/&gt;
        &lt;set-variable variableName="errorReasonPhrase" value="Table name does not exist" doc:name="Set reason phrase"/&gt;
        &lt;message-properties-transformer doc:name="Message Properties"&gt;
        &lt;add-message-property key="timestamp" value="#[server.dateTime.format('yyyy-MM-dd hh:mm:ss.SS')]"/&gt;
        &lt;add-message-property key="messageID" value="#[message.id]"/&gt;
        &lt;add-message-property key="status" value="Error"/&gt;
        &lt;add-message-property key="executionPoint" value="Error Handling"/&gt;
        &lt;add-message-property key="ip" value="#[server.ip]"/&gt;
        &lt;add-message-property key="serverName" value="#[server.host]"/&gt;
        &lt;add-message-property key="domainname" value="xyz.com"/&gt;
        &lt;add-message-property key="errorCode" value="123"/&gt;
        &lt;add-message-property key="errorType" value="Bad request"/&gt;
        &lt;add-message-property key="errorText" value="ComponentException"/&gt;
        &lt;add-message-property key="integrationName" value="${integrationName}"/&gt;
        &lt;add-message-property key="resourceType" value="HTTP Request"/&gt;
        &lt;add-message-property key="resourceName" value="Payload"/&gt;
    &lt;/message-properties-transformer&gt;
        &lt;logger level="INFO" message="#[LoggingAppendString] - Failure" doc:name="Logger"/&gt;

    &lt;/rollback-exception-strategy&gt;

&lt;/choice-exception-strategy&gt;
</code></pre>

<p>Which is working fine. But when I'm replacing it with Catch Exception Strategy as shown below :</p>

<pre><code>&lt;choice-exception-strategy name="MyExptn"&gt;
&lt;catch-exception-strategy when="#[exception.causedBy(org.mule.component.ComponentException)]" doc:name="ComponentException Strategy"&gt;

    &lt;set-variable variableName="errorTableNotPresent" value="400" doc:name="Set status code"/&gt;
    &lt;set-variable variableName="errorReasonPhrase" value="Table name does not exist" doc:name="Set reason phrase"/&gt;
    &lt;message-properties-transformer doc:name="Message Properties"&gt;
    &lt;add-message-property key="timestamp" value="#[server.dateTime.format('yyyy-MM-dd hh:mm:ss.SS')]"/&gt;
    &lt;add-message-property key="messageID" value="#[message.id]"/&gt;
    &lt;add-message-property key="status" value="Error"/&gt;
    &lt;add-message-property key="executionPoint" value="Error Handling"/&gt;
    &lt;add-message-property key="ip" value="#[server.ip]"/&gt;
    &lt;add-message-property key="serverName" value="#[server.host]"/&gt;
    &lt;add-message-property key="domainname" value="xyz.com"/&gt;
    &lt;add-message-property key="errorCode" value="123"/&gt;
    &lt;add-message-property key="errorType" value="Bad request"/&gt;
    &lt;add-message-property key="errorText" value="ComponentException"/&gt;
    &lt;add-message-property key="integrationName" value="${integrationName}"/&gt;
    &lt;add-message-property key="resourceType" value="HTTP Request"/&gt;
    &lt;add-message-property key="resourceName" value="Payload"/&gt;
&lt;/message-properties-transformer&gt;
    &lt;logger level="INFO" message="#[LoggingAppendString] - Failure" doc:name="Logger"/&gt;

&lt;/catch-exception-strategy&gt;

&lt;/choice-exception-strategy&gt;
</code></pre>

<p>Will only log the message but HTTP status always showing 200 OK.</p>

<p>My basic requirement is to send status code based on the error. Say for example if request is not correct then Bad request with code 400. </p>