Skip to main content
Contact Us 1-800-596-4880

How to retrieve custom headers, query and URI parameters using DataWeave

5 min read

Overview

When you are building out an API and integration with MuleSoft’s Anypoint Studio, you will need to capture custom headers, query parameters, and URI parameters from the HTTP request. Capturing this data is important and it’s extremely easy to capture using the Transform Message component and DataWeave. In the tutorial below, we are going to highlight how to retrieve custom headers, queries, and URI parameters from your HTTP request using DataWeave.

If you haven’t already, make sure to sign up for a free Anypoint Platform account. Click the button below to create a free account.

Start free trial

Already have an account? Sign in.

Capturing HTTP headers in DataWeave

When you send an HTTP request to your HTTP endpoint, you can capture the header data using a DataWeave script. In the case of the DataWeave script below, we will return the header information in the body of the response. You can use DataWeave to save these responses as variables, and pass them to other systems as needed.

1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"client_id": attributes.headers.client_id,
"client_secret": attributes.headers.client_secret
}

Data weave examples

Headers

You can see that after sending the POST request, we receive a 200 OK response from our mule application returning our desired response in JSON.

Capturing query parameters in DataWeave

DataWeave makes it easy to capture query parameters and save them for later use within your integration. Use the attributes.queryParams reference in DataWeave to capture the query parameter sent in the request.

1
2
3
4
5
6
7
%dw 2.0
output application/json
---
{
"name" attributes.queryParams.name,
"productID": attributes.queryParams.productID
}

screenshot attributes

screenshot attributes

Capturing URI parameters in DataWeave

You can capture a URI parameter (also known as a Path Param) using DataWeave. A URI parameter identifies a specific resource whereas a Query Parameter is used to sort or filter resources. When you are setting up your HTTP listener, make sure to set up your endpoint using the curly brace notation to capture the variable element. Then use the DataWeave command attributes.uriParams to capture the URI parameter from the HTTP request.

1
2
3
4
5
6
%dw 2.0
output application/json
---
{
"uuid": attributes.uriParams.'uuid'
}

attributes 3

attributes 4

attributes 5

Here is another example of how to format your URI Parameters if you are sending an outbound request

URI Parameters

Expand your DataWeave knowledge

Want to learn more about DataWeave? Read the tutorial on how to setup and use the DataWeave Playground by clicking here! Also, make sure to visit our developer tutorials page for more tutorials on DataWeave and MuleSoft development.

Try Anypoint Platform for free

Start your 30-day free trial of the #1 platform for integration, APIs, and automation. No credit card required. No software to install.

Try for free

anypoint product trial zigzag