Contact Free trial Login

HTTP Error Status Code and Reason Phrase - Mule 4

The following Mule application example helps you to set the error status code and reason phrase for a user login validation request. To view the status code and explanation of an HTTP request, use a browser extension such as Postman (Google Chrome), or the curl command line utility.
The example expects to receive requests in the address of http://localhost:8081/login/, these requests must contain a query parameter named user. Depending on the value of this parameter, one of two things may occur:

  • When the parameter equals to user=mule:

    • The validation operation evaluates the parameter as true.

    • A set payload transformer sets the message payload to a success message.

    • The HTTP Connector sets variables in Status Code and Reason as 200 Log in Successful!.

  • When the parameter equals to user=anythingElse:

    • The validation operation assertion on the parameter fails.

    • The flow calls an error handler.

    • The HTTP Connector sets the variables in Status Code and Reason as 404 Requested user does not exist

Note that in either case, the request response doesn’t display as the response body, so it won’t display if you make your request through a browser window.

Set Error Status Code and Reason Phrase in Studio

In Studio, create a new Mule project in which to add and configure the connector:

  1. Select File > New > Mule Project.

  2. Enter a name for your Mule project and click Finish.

  3. In the Mule Palette view, select HTTP > Listener.

  4. Drag Listener to the Studio canvas.

  5. On the Listener configuration screen, optionally change the value of the Display Name field.

  6. Set Path to /login.

  7. Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of HTTP Listener in the app.

  8. On the General tab, set Host to localhost, and leave Port as the default value of 8081.

  9. Click OK.

In Studio, the configuration looks as follows:

HTTP Listener Configuration
Figure 1. HTTP Listener Configuration
  1. On the Response section of the Responses tab of the HTTP Listener, set the Status Code to 200 and the Reason phrase to Login Successful.

  2. On the Error Response section, set the Status Code to #[vars.errorStatusCode] and the Reason phrase to #[vars.errorReasonPhrase].

In Studio, the configuration looks as follows:

HTTP Error Status Code and Reason
Figure 2. HTTP Error Status Code and Reason
  1. In your flow, after HTTP Listener, add a Validation: Is True operation.

  2. On the General section of the General tab of the Validation Module, select Expression from the dropdown menu, and to validate the user query parameter, add the following code in the expression field box:

#[attributes.queryParams.user == 'mule']

In Studio, the configuration looks as follows:

Validate Module Configuration
Figure 3. Validation Module Configuration
  1. In your flow, after the Validation Module operation, add a Set Payload transformer, and set the message payload Value to Log in Successful!

In Studio, the configuration looks as follows:

Set Payload Configuration
Figure 4. Set Payload Configuration
  1. On the Error Handling section of your flow, add an On Error Propagate component.

  2. On the On Error Propagate component, add two Set Variable transformers:

    • On the first variable, set Display Name to Set Status Code, Name to errorStatusCode and Value to 404.

    • On the second variable, set Display Name to Set Reason Phrase, set Name to errorReasonPhrase and Value to Requested user does not exist.

In Studio, the configuration looks as follows:

Error Handling Configuration
Figure 5. Error Handling Configuration
  1. In Studio, save all your project changes, and then click the Run button to deploy the application.

  2. In cURL, or your internet browser extension (such as Postman), test the example http://localhost:8081/login?user=mule

If the user is valid, the result returns 200 Log in Successful! otherwise, returns 404 Requested user does not exist.

Set Error Status Code and Reason Phrase in XML

If you use a XML editor, or the Configuration XML tab in Studio, the XML looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
                          http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
                          http://www.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.xsd">
    <http:listener-config name="HTTP_Listener_config" >
        <http:listener-connection host="localhost" port="8081" />
    </http:listener-config>
    <flow name="customResponseFlow" >
        <http:listener doc:name="Listener" path="/login" config-ref="HTTP_Listener_config">
            <http:response statusCode="200" reasonPhrase="Login Successful" />
            <http:error-response statusCode="#[vars.errorStatusCode]" reasonPhrase="#[vars.errorReasonPhrase]" />
        </http:listener>
        <validation:is-true expression="#[attributes.queryParams.user == 'mule']"/>
        <set-payload value="Log in Successful!" />
        <error-handler name="Error_Handler" >
            <on-error-propagate enableNotifications="true" logException="true" >
                <set-variable value="404" variableName="errorStatusCode"/>
                <set-variable value="Requested user does not exist" variableName="errorReasonPhrase"/>
            </on-error-propagate>
        </error-handler>
    </flow>
</mule>