Contact Us 1-800-596-4880

Post a Form Example - Mule 4

The following example shows how to configure Anypoint Connector for HTTP (HTTP Connector) Listener source to post a form by sending HTTP requests, which contains form parameters. To accomplish this example, you must create the Mule app, configure an HTTP global element, run the app, and post the form using curl commands.

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

Post a Form flow
Figure 1. Post a Form 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 submitform.

  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.

  6. Drag a Choice router component to the right of the HTTP Listener source.
    The Choice router adds conditional processing to the flow.

  7. Drag a Set Payload component into the When condition box of the Choice router.

  8. Set the Value field to Sorry #[payload.'firstname'], you’re too young to register.
    Note that this expression refers to one of the form parameters included in the request: firstname. After the connector receives the parameter, it exists in the Mule Message payload as an item within a map.

  9. On the Choice router, select the When condition.

  10. Set the Expression field to #[server.dateTime.year-18 < payload.'yearborn']
    This expression refers to one of the incoming form parameters, yearborn.

  11. Drag another Set Payload component into the Default condition box of the Choice router.

  12. Set the Value field to Registration has been carried out successfully! Welcome #[payload.'firstname'] #[payload.'lastname']!

  13. Save your Mule app.

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

  15. To post the form, try the following curl command http://localhost:8081/submitform.

The post body contains the following form parameters: firstname, lastname and yearborn:

POST /submitform HTTP/1.1

User-Agent: Mule/3.6
Content-Type: application/x-www-form-urlencoded
Content-Length: 32

firstname=Aaron&lastname=Aguilar+Acevedo&yearborn=1999

When the HTTP Listener receives the request, it creates a Mule Message that contains a map type payload with the following key-value pairs:

firstname: Aaron
lastname: Aguilar Acevedo
yearborn: 1999

Note that in the case of the lastname parameter, while the value is encoded in the HTTP request (Aguilar+Acevedo), the connector automatically decodes the value for you when placing it in the Mule Message.

On any of the remaining blocks on the flow, you can easily access the value of the elements in the Map payload by using MEL expressions to refer to their corresponding keys.

In the previous example, the value matching the yearborn key is obtained via the expression #[payload.'yearborn']. Depending on the value of this expression, one of two different paths is followed:

  • The first path sets the payload to a message that rejects the registration, referencing the value matching the firstname key through a similar MEL expression.

  • The second path accepts the registration and welcomes the user by name, referencing the firstname and lastname values.

XML for Posting a Form App

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:spring="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">


    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="RegisterUser">
        <http:listener config-ref="HTTP_Listener_Configuration" path="submitform" doc:name="HTTP"/>
        <choice doc:name="Choice">
            <when expression="#[server.dateTime.year-18 &lt; payload.'yearborn']">
                <set-payload value="Sorry #[payload.'firstname'], you're too young to register." doc:name="Too young"/>
            </when>
            <otherwise>
                <set-payload value="Registration has been carried out successfully! Welcome #[payload.'firstname'] #[payload.'lastname']!" doc:name="Success"/>
            </otherwise>
        </choice>
    </flow>
</mule>
View on GitHub