Contact Us 1-800-596-4880

DataWeave Request Filter Policy

Policy name

DataWeave Request Filter

Summary

Filters requests by using a DataWeave script

Category

Transformation

First Flex Gateway version available

1.12

Returned Status Codes

400 - The DataWeave script failed to execute or returned an invalid result

Summary

The DataWeave Request Filter policy evaluates a DataWeave script on the request, including headers and the body, to allow or block the message. The script returns an object with a Boolean success value. If success is false, the script can return a response object with statusCode, body, and headers. The gateway sends that as the immediate HTTP response to the client and stops processing. If success is true, the message continues.

Result format:

  • Success: { "success": true }

  • Error: { "success": false, "response": { "body": "…​", "headers": {}, "statusCode": 400 } }

Configure Policy Parameters

Flex Gateway Local Mode

- policyRef:
    name: dataweave-request-filter-flex
  config:
    script: <string>           // REQUIRED - DataWeave script
    requiresPayload: <boolean> // OPTIONAL, default: false
Parameter Required Default Value Description

script

Required

A DataWeave script that returns an object with a boolean success field. If success is false, the optional response is: { "body": string, "headers": object, "statusCode": number }.

requiresPayload

Optional

false

If true, the script receives the message body in the payload binding. If true and the body is missing, the gateway returns an error.

Resource Configuration Example

  policies:
    - policyRef:
        name: dataweave-request-filter-flex
      config:
        script: |
          %dw 2.0
          output application/java
          ---
          if (attributes.headers["x-api-key"] != null) { success: true }
          else { success: false, response: { statusCode: 401, body: "Missing API key", headers: {} } }
        requiresPayload: false

Managed Flex Gateway and Flex Gateway Connected Mode

When you apply the policy to your API instance from the UI, the following parameters are displayed:

Parameter Description

Policy type

DataWeave Request Filter

Script

The DataWeave script that returns a success or error result

Requires payload

Whether the script requires the message body

Method & Resource conditions

The option to add configurations to only a select few or all methods and resources of the API