DataWeave 2 Basics
home
Use the six flows in this example to understand the basic implementations of DataWeave 2 for different components. For example, this flow shows how a service saves a CSV file with user data while returning a greeting. The call to the service includes two parameters, username and age, which the service stores.
Assumptions
You are familiar with DataWeave 2 Language's basic syntax and are comfortable building and running Mule applications using Mule Studio or XML.
If you aren't yet familiar with how to access information about the Mule messages that pass through your applications, consider following this tutorial, which walks you through both examining your Mule message and its data structure and writing DataWeave 2 expressions.
Set Up and Run the Example
- Open the Example project in Anypoint Studio from Anypoint Exchange.
- In your application in Studio, click the Global Elements tab. Double-click the HTTP Listener global element to open its Global Element Properties panel. Change the contents of the
port
field to required HTTP port, for example, 8081. - In the Package Explorer pane in Studio, right-click the project name, then select Run As > Mule Application. Studio runs the application and Mule is up and kicking!
- Flow 1: In a web browser, access the URL
http://localhost:8081/greet1?username=yourName
. The browser displays the words Hello (yourName). - Flow 2: In a web browser, access the URL
http://localhost:8081/greet2?username=yourName
. The browser displays Hello (yourName). Then, access the URL again, but this time do not include any parameters. Verify that the expected output is received. - Flow 3: In a web browser, access the URL
http://localhost:8081/greet3?username=yourName&age=22
. This prints the words Hello (yourName) in your browser and also save a CSV file that contains this data, plus the valuetrue
for the Boolean parameter. - Flow 4: In a browser, access the URL
http://localhost:8081/greet4?username=yourName&age=22
. This prints the words Hello (yourName) in your browser and also save a CSV file that contains this data, plus the valuetrue
for the Boolean parameter. - Flow 5: Send the HTTP endpoint an HTTP request that includes a body with an attached XML file. Send a POST request to
http://localhost:8081/greet5
, attaching an XML file to the body of the message. Sample XML is provided below.To do this, send a POST request via a browser extension such as Postman (for Google Chrome) or the cURL command line utility.< user > < username > test < /username > < age > 21 < /age > < /user >
The response prints Hello (yourName) in the browser and also saves a CSV file that contains this data, plus the valuetrue
for the Boolean parameter. - Flow 6: Send the HTTP endpoint an HTTP request that includes a body with an attached JSON file. Send a POST request to
http://localhost:8081/greet6
, attaching a JSON object to the body of the message. A sample JSON is provided below. To do this, send a POST via a browser extension such as Postman (for Google Chrome) or the cURL command line utility.{ "username": "test", "age" : 21 }
The response writes Hello (yourName) in the browser and also saves a CSV file that contains this data, plus the valuetrue
for the Boolean parameter.
About Each Flow
Flow 1 – Accessing Properties
This flow creates a simple web service that takes an HTTP request that includes a username
parameter and returns a greeting using that username.
DataWeave performs two tasks:
- Accesses query parameter
- Dynamically set the payload
Flow 2 – Dynamic Routing by Evaluating a Condition
In the previous flow, if your request to the service doesn't include a username
parameter, an error is returned. You can prevent this by adding some flow control components. This flow includes a choice router that verifies if the required parameter is being passed.
DataWeave performs three tasks:
- Evaluates conditions in a choice component
- Accesses a query parameter
- Dynamically sets the payload
Flow 3 – Variable Assignment and Evaluating Conditions
In this flow, the service saves a CSV file with user data and returns a greeting. The request to the service includes two parameters, username
and age
. The service stores these two parameters.
DataWeave performs four tasks:
- Sets a flow variable in the message
- Generates an output that is based on evaluating the input
- Accesses a query parameter
- Dynamically sets the payload
Flow 4 – Creating Maps and Evaluating Conditions
In this flow, as in the previous one, the Mule application saves a CSV file with user data and returns a greeting. The request to the service includes two parameters, username
and age
. The service stores these two parameters.
DataWeave performs four tasks:
- Sets a flow variable in the message
- Generates an output based on evaluating the input within DataWeave
- Accesses a query parameter
- Dynamically sets the payload
Flow 5 – XML Request
In all the previous flows, calls to the service were made via HTTP GET requests that included query parameters. In this example, the service you create is an API that accepts POST requests with XML bodies. The required XML includes two parameters, username
and age
. The service stores these two parameters.
DataWeave performs four tasks:
- Sets a flow variable in the message
- Generates output based on evaluating the input
- Parses an XML file
- Dynamically sets the payload
Flow 6 – JSON Request
This flow is like Flow 5, except that the service now receives JSON inputs rather than XML.
The JSON input includes two parameters, username
and age
. The service stores these two parameters.
DataWeave performs three tasks:
- Sets a flow variable in the message
- Generates an output based on evaluating the input
- Dynamically sets the payload