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

dataweave header
Jordan Schuetz
Developer Advocate
5 min read

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.

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.

client idpostman

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.

screenshot attributesscreenshot attributes 2
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.

attributes 3attributes 4attributes 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 free trial

Already have an account? Sign in.

Related tutorials