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:
Create the Mule Application
To create the Mule flow:
-
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. -
Set the Path field to
requests
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
-
Base path:
mypath
-
-
Click OK.
-
Drag a Logger component to the right of the HTTP Listener source.
-
Set the Message field to
flow 1
. -
Create a second flow by dragging another HTTP Listener source below the first flow.
-
Set the Connector Configuration field to
HTTP_Listener_Config
, the global element you created for the first Listener. -
Set the Path field to
requests
. -
In the connector configuration screen, select the Advanced tab, and set the Allowed methods field to
POST
. -
Drag another Logger component to the right of the HTTP Listener source.
-
Set the Message field to
flow 2
. -
Save your Mule app.
-
Click the project name in Package Explorer and then click Run > Run As > Mule Application.
-
In your browser extension send an HTTP
GET
andPOST
requests tohttp://localhost:8081/mypath/requests
.The
GET
request response isflow 1
. ThePOST
request response isflow 2
. -
Send a
DELETE
request.The
405 Method Not Allowed
message is returned becauseDELETE
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>