#[attributes.headers.'my-header']
DataWeave Support in Flex Gateway Policies
DataWeave is the programming language designed by MuleSoft for data transformation. It enables you to build a simple solution for a common integration developer use case: read and parse data from one format, transform the data, and write it out as a different format.
Flex Gateway supports a subset of DataWeave in policy configuration expressions. DataWeave is only supported in included policies and custom policies created with PDK..
Unavailable Value Constructors for Types
-
Object(Objects can not be defined as literals)
Available Functions
fromBase64
|
Flex Gateway doesn’t support DataWeave expressions with binary type results. To use the |
Available Predefined Variables
-
-
attributes.headers -
attributes.method(Only available in request context) -
attributes.queryParams(Only available in request context) -
attributes.queryString(Only available in request context) -
attributes.requestPath(Only available in request context) -
attributes.requestUri(Only available in request context) -
attributes.localAddress(Only available in request context) -
attributes.remoteAddress(Only available in request context) -
attributes.scheme(Only available in request context) -
attributes.version(Only available in request context) -
attributes.statusCode(Only available in response context)
-
-
-
authentication.clientId -
authentication.clientName -
authentication.principal -
authentication.propertiesTo learn more about how to access the authentication object, see Access the Authentication Object.
-
-
payload-
Fields depend on the current payload. For example,
#[payload]returns the body of the message.
-
-
-
Individual policies support different sets of variables. For information, refer to the policies listed in Inbound Policies Directory.
-
DataWeave Usage Examples
You can use DataWeave to extract different values from the request and response.
| The Flex Gateway Policy Development Kit (PDK) provides the DataWeave example policy that you can use to test DataWeave expressions. |
For example, use DataWeave expressions to:
-
Read a header:
-
Ensure a specific header is defined:
#[attributes.headers.'my-header' != null]
-
Check if a request has basic authorization:
#[lower(splitBy(attributes.headers.authorization default '', ' ')[0]) == 'basic']
-
Check if a request contains
/somepath:#[attributes.requestPath contains '/some']
-
Check if a request doesn’t contain
/somepath:#[not(attributes.requestPath contains '/some')]
-
Read the username from a
Basicauthorization header:#[splitBy(dw::core::Binaries::fromBase64(dw::core::Strings::substringAfter(attributes.headers.authorization default '', 'Basic')), ':')[0]]
-
Read the password from a
Basicauthorization header:#[splitBy(dw::core::Binaries::fromBase64(dw::core::Strings::substringAfter(attributes.headers.authorization default '', 'Basic ')), ':')[1]]
-
Read the first value of the
x-forwarded-forheader:#[trim(splitBy(attributes.headers.'x-forwarded-for', ',')[0])]
-
Read the last value of the
x-forwarded-forheader:#[trim(splitBy(attributes.headers.'x-forwarded-for', ',')[-1])]
-
Read the value from a
Cookie:#[trim(dw::core::Strings::substringBefore(dw::core::Strings::substringAfter(attributes.headers.Cookie default '', 'myCookieName='), ';'))]
-
Check if a single value or space-separated header contains a specific value:
#[splitBy(attributes.headers.scopes default '', ' ') contains 'read']
Access the Authentication Object
To access the authentication object, you must first configure an authentication policy that populates the object.
The authentication policy you apply affects the available authentication object properties:
-
Multiple policies can validate Client ID:
-
OAuth 2.0 Token Introspection Policy (If Client ID enforcement is configured)
-
OpenID Connect OAuth 2.0 Token Enforcement Policy (If Client ID enforcement is configured)
-
JWT Validation Policy (If Client ID enforcement is configured)
Policies validating Client ID provide these authentication object properties:
-
authentication.clientName: Name of the contract’s client application -
authentication.clientId: ID of the contract’s client application -
authentication.properties.slaId: SLA ID assigned to the contract
-
-
-
authentication.properties.claims.<claimName>: All claims provided by the JWT
-
-
Multiple policies validate basic authentication credentials:
-
Basic Authentication: LDAP Policy
Policies validating basic authentication credentials provide these authentication object properties:
-
authentication.principal: Username passed as basic auth user
-
After your policy populates the authentication object, you can access the object in policies ordered after the authentication policy using DataWeave expressions. For example:
-
Read a value from the authentication object:
#[authentication.properties.claims.exp]
-
Check if a JSON array or a JSON string JWT claim contains a specific value:
#[authentication.properties.claims.custom_claim contains 'read']



