---
schema_version: "1"
id: c71e9dfc3c66aa20f49867fdba2a844099eafeca465505d1c563fe51b515acf0
title: HTTP Error Status Code and Reason Phrase Example
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/http-connector/latest/http-error-status-reason-phrase-task.md
canonical_url: https://docs.mulesoft.com/http-connector/latest/http-error-status-reason-phrase-task
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 32d65d2db7e6cf613487473a47be6411e5948391d9f9f26ebc07fe076353ac72
description: MuleSoft Documentation Site
release: "1.12"
latest: true
---
---
product: HTTP Connector
version: 1.12
is-latest-version: true
---

# HTTP Error Status Code and Reason Phrase Example

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The following example illustrates how to configure Anypoint Connector for HTTP (HTTP Connector)
**Listener** source, so you can 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, or the [curl](https://curl.se/) 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. To accomplish this example, you must
create the Mule app, configure an HTTP global element, run, and test the app with curl commands.

The following screenshot shows the Anypoint Studio app flow for this example:

![HTTP Error Status Code and Reason Phrase Flow](/mulesoft/current/en-us/http-connector/latest/_images/http-error-flow.png)

## Create the Mule Application

To create the Mule flow:

1.  In the **Mule Palette** view, select the HTTP **Listener** source and drag it on to the canvas.

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

3.  Set **Path** to `/login`.

4.  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.

5.  On the **General** tab, configure the following fields:

    - **Host**: `localhost`

    - **Port**: `8081`

6.  Click **OK**.

![HTTP Listener Configuration](/mulesoft/current/en-us/http-connector/latest/_images/http-error-studio-1.png)

7.  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`.

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

![HTTP Error Status Code and Reason](/mulesoft/current/en-us/http-connector/latest/_images/http-error-studio-2.png)

9.  Drag a **Validation: Is True** operation after the HTTP **Listener** source.

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

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

![Validate Module Configuration](/mulesoft/current/en-us/http-connector/latest/_images/http-error-studio-3.png)

11. Drag a **Set Payload** transformer after the **Validation: Is True** operation, and set the
    **Value** field to `Log in Successful!`

![Set Payload Configuration](/mulesoft/current/en-us/http-connector/latest/_images/http-error-studio-4.png)

Figure 1. Set Payload Configuration

11. On the **Error Handling** section of your flow, add an **On Error Propagate** component.

12. 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`.

![Error Handling Configuration](/mulesoft/current/en-us/http-connector/latest/_images/http-error-studio-5.png)

13. Save your Mule app.

14. Click the project name in **Package Explorer** and then click **Run** > **Run As** > **Mule
    Application**.

15. In cURL test the URL
    `[http://localhost:8081/login?user=mule](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`.

## XML for HTTP Error Status Code and Reason Phrase

Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule
app:

```xml
<?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>
```

## See Also

- [Introduction to Anypoint Connectors](https://docs.mulesoft.com/connectors/introduction/introduction-to-anypoint-connectors)

- [Salesforce Help](https://help.salesforce.com)
