Route HTTP Requests to Different Paths Example - Mule 4
The following example has multiple flows, each having an Anypoint Connector for HTTP (HTTP Connector) Listener source that listens for requests to different subpaths:
-
HTTP requests directed to
http://localhost:8081/account
are routed to the first flow. -
HTTP requests directed to
http://localhost:8081/employee
are routed to the second flow. -
HTTP requests that don’t match the path defined in any of the HTTP Listeners make the connector return an HTTP
404 Resource Not Found
error.
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:
Create the Mule Application
To create the Mule flow:
-
In the Mule Palette view, select the HTTP Listener source and drag it onto the canvas.
The source initiates the flow by listening for incoming HTTP message attributes. -
Set the Path field to
account
to start the app from the web browser. -
Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of the HTTP Listener in the app.
-
On the General tab, configure the following fields:
-
Host:
localhost
-
Port:
8081
-
-
Click OK.
Figure 2. HTTP Listener global configurationFigure 3. HTTP Listener configuration -
Drag a Logger component to the right of the HTTP Listener source.
-
To create the second flow, drag another HTTP Listener below the first flow.
-
For the Connector configuration field, select the same
HTTP_Listener_config
configuration created for the first HTTP Listener source. -
Set the Path field to
employee
.Figure 4. HTTP Listener configuration -
Drag a Logger component to the right of the second HTTP Listener source.
-
Save your Mule app.
-
Click the project name in Package Explorer and then click Run > Run As > Mule Application.
-
Open a browser window and type the first URL:
http://localhost:8081/account
. -
Open another browser window and type the second URL:
http://localhost:8081/employee
. -
Verify that the logs show the requests routed to the first and then the second flows respectively.
XML for Routing HTTP Requests
Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule app:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
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:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
<http:listener-connection host="localhost" port="8081" />
</http:listener-config>
<flow name="Flow1" >
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="path"/>
<logger level="INFO" doc:name="Logger" />
</flow>
<flow name="Flow2" >
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="employee"/>
<logger level="INFO" doc:name="Logger" />
</flow>
</mule>