Contact Us 1-800-596-4880

Route Requests Based on an HTTP Method Example - Mule 4

The following example illustrates how to configure Anypoint Connector for HTTP (HTTP Connector) Listener operation to route requests based on an HTTP method. 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:

Route Requests Based on an HTTP Method flow

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.
    The source initiates the flow by listening for incoming HTTP message attributes.

  2. Set the Path field to requests 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

    • Base path: mypath

  5. Click OK.

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

  7. Set the Message field to flow 1.

  8. Create a second flow by dragging another HTTP Listener source below the first flow.

  9. Set the Connector Configuration field to HTTP_Listener_Config, the global element you created for the first Listener.

  10. Set the Path field to requests.

  11. In the connector configuration screen, select the Advanced tab, and set the Allowed methods field to POST.

  12. Drag another Logger component to the right of the HTTP Listener source.

  13. Set the Message field to flow 2.

  14. Save your Mule app.

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

  16. In your browser extension send an HTTP GET and POST requests to http://localhost:8081/mypath/requests.

    The GET request response is flow 1. The POST request response is flow 2.

  17. Send a DELETE request.

    The 405 Method Not Allowed message is returned because DELETE is not an allowed method.

XML for Routing Requests Based on an HTTP Method

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"  basePath="mypath" >
		<http:listener-connection host="localhost" port="8081" />
	</http:listener-config>
	<flow name="Flow1" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="requests"/>
		<logger level="INFO" doc:name="Logger" message="flow 1"/>
	</flow>
	<flow name="Flow2" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="requests" allowedMethods="POST"/>
		<logger level="INFO" doc:name="Logger" message="Flow 2"/>
	</flow>
</mule>
View on GitHub