Contact Us 1-800-596-4880

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:

HTTP Requests to paths flow

Create the Mule Application

To create the Mule flow:

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

  2. Set the Path field to account to start the app from the web browser.

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

  4. On the General tab, configure the following fields:

    • Host: localhost

    • Port: 8081

  5. Click OK.

    HTTP Listener global configuration
    HTTP Listener configuration
  6. Drag a Logger component to the right of the HTTP Listener source.

  7. To create the second flow, drag another HTTP Listener below the first flow.

  8. For the Connector configuration field, select the same HTTP_Listener_config configuration created for the first HTTP Listener source.

  9. Set Path to employee.

    HTTP Listener configuration
  10. Drag a Logger component to the right of the second HTTP Listener source.

  11. Save your Mule app.

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

  13. Open a browser window and type the first URL: http://localhost:8081/account.

  14. Open another browser window and type the second URL: http://localhost:8081/employee.

  15. 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>
View on GitHub