---
schema_version: "1"
id: 5da527df187481caa39ccd4562c106c0b87018edeac783daf62470c4def9d079
title: Using DataWeave Expressions
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/pdk/latest/policies-pdk-configure-features-dataweave.md
canonical_url: https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-dataweave
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2f1746cfb7cc138e43a0330d990fe192c3329ddf6d7d668ffc8b2c6fa4b76743
description: MuleSoft Documentation Site
release: "1.10"
latest: true
---
# Using DataWeave Expressions

<table><tbody><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content">To view an example policy project that uses DataWeave expressions, see <a href="https://github.com/mulesoft/pdk-custom-policy-examples/blob/1.10.0/dataweave/README.md" target="_blank" rel="noopener">DataWeave Policy Example</a>.</td></tr></tbody></table>

If the input DataWeave expression parameter is valid, the policy parses the expression and transforms it into a `pdk::script::Script` expression. To obtain the result of the script:

1. Create an evaluator.
2. Call the binding function for each parameter defined in the schema definition:
    
    - `vars`: Call the `bind_vars` method for each of the policy’s `vars`. After all of `vars` are bound, the evaluator tries to solve the expression. If the expression contains `vars` other than the ones defined in the schema, they resolve to `null`.
    - `attributes`: Call the `bind_attributes` method with an implementation of the `AttributesBinding` trait. Omni Gateway Policy Development Kit (PDK) provides an implementation of this trait by instantiating a new `HandlerAttributesBinding` for each `RequestHeaderState`, `RequestHeaderState`, and response of an [HTTP call](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-http-request).
    - `authentication`: Call the `bind_authentication` method with an implementation of the trait `AuthenticationBinding`. PDK provides an implementation of this trait for `AuthenticationData`. To inject authentication information into your policy, see [Inject Authentication Inforamaion](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-inject-parameters#authentication).
    - `payload`: Call the `bind_payload` method with an implementation of the trait `PayloadBinding`. PDK provides an implementation of this trait for `RequestBodyState`, `ResponseBodyState`, and the response of an [HTTP call](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-http-request).
        
        <table>
<tbody><tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>Call only the methods for the bindings defined in the schema definition. For example, if the <code>attributes</code> binding is not used, don’t call the <code>bind_attributes</code> method.</p>
</div>
<div class="paragraph">
<p>To learn more about defining DataWeave expressions, see <a href="https://docs.mulesoft.com/pdk/latest/policies-pdk-create-schema-definition#dataweave-expressions" class="xref page">DataWeave Expressions</a>.</p>
</div>
</td>
</tr>
</tbody></table>
3. After you bind all expression variables, obtain the result by calling the `eval` method.

## [](#example-evaluator-code)Evaluate DataWeave Expressions

For example, to evaluate the `tokenExtractor` DataWeave expression in the following policy schema definition:

```yaml
apiVersion: gateway.mulesoft.com/v1alpha1
kind: Extension
metadata:
 labels:
   title: jwt-auth
   category: Custom
spec:
 extends:
   - name: extension-definition
     namespace: default
 properties:
   tokenExtractor:
     type: string
     format: dataweave
     default: "#[vars.myVar]"
     bindings:
       payload:
         mimeType: text
       attributes: true
       authentication: true
       vars:
         - myVar
 required:
   - tokenExtractor
```

<table>
<tbody><tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>Omni Gateway doesn’t support DataWeave expressions with binary type results. To use the <code>dw::Binaries::fromBase64('dXNlcjpwYXNz')</code> function, transform the result to a string. Transform the binary output to a string with the <code>dw::util::Coercions::toString(binary: Binary, encoding: String): String</code> function. For example, <code><mark>[dw::util::Coercions::toString(dw::core::Binaries::fromBase64('dXNlcjpwYXNz'), 'UTF-8')]</mark></code>. If using a different function that transforms the binary by default, such as <code>[splitBy(dw::core::Binaries::fromBase64('dXNlcjpwYXNz'), ':')]</code>, you can skip manually transforming the binary.</p>
</div>
</td>
</tr>
</tbody></table>

Use the following Rust snippet:

```rust
// Copyright 2024 Salesforce, Inc. All rights reserved.
mod generated;


use anyhow::Result;


use crate::generated::config::Config;
use pdk::authentication::{Authentication, AuthenticationHandler};
use pdk::logger::info;
use pdk::hl::*;
use pdk::script::{Evaluator, HandlerAttributesBinding, Value};


async fn request_filter(
   state: RequestState,
   stream: StreamProperties,
   auth: Authentication,
   mut evaluator: Evaluator<'_>,
) {
   evaluator.bind_vars("myVar", "myVal");

   evaluator.bind_authentication(&auth.authentication());

   let state = state.into_headers_state().await;
   evaluator.bind_attributes(&HandlerAttributesBinding::new(state.handler(), &stream));

   let state = state.into_body_state().await;
   evaluator.bind_payload(&state);

   if let Ok(value) = evaluator.eval() {
       match value {
           Value::Null => info!("value was null!"),
           Value::Bool(val) => info!("value was Bool: {val}"),
           Value::Number(val) => info!("value was Number: {val}"),
           Value::String(val) => info!("value was String: {val}"),
           Value::Array(val) => info!("value was Array: {val:?}"),
           Value::Object(val) => info!("value was Object: {val:?}"),
       }
   }
}


#[entrypoint]
async fn configure(launcher: Launcher, Configuration(bytes): Configuration) -> Result<()> {
   let config: Config = serde_json::from_slice(&bytes)?;


   launcher
       .launch(on_request(|request, stream, auth| {
           request_filter(request, stream, auth, config.token_extractor.evaluator())
       }))
       .await?;
   Ok(())
}
```

## [](#validate-the-expression-was-resolved)Validate the Expression was Resolved

You can resolve some expressions before all values are bound. For example, the expression `#[vars.myVar]` does not require the `attributes`, `payload`, no `authentication` to be resolved.

After one of the binding methods is called, you can confirm the expression was completely resolved by calling the method:

```rust
evaluator.is_ready()
```

## [](#see-also)See Also

- [Implementing Your Custom Policy Features in Rust](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features)