Contact Us 1-800-596-4880

DataWeave Body Transformation Policy

Policy name

DataWeave Body Transformation

Summary

Transforms the body of request or response traffic with a DataWeave script

Category

Transformation

First Omni Gateway version available

v1.12.0

Release Notes

DataWeave Body Transformation

Returned Status Codes

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

Summary

The DataWeave Body Transformation policy transforms request or response body content with a DataWeave script. The script receives attributes.headers and payload. If the script declares an output MIME type, such as output application/json, the gateway uses it for the transformed response. Otherwise, the gateway keeps the original Content-Type. The gateway updates Content-Type after transformation.

Payload Handling

The DataWeave Body Transformation policy processes only the side that requestFlow sets. Set onRequest to transform the request or onResponse to transform the response. The other side passes through unchanged.

The policy buffers the full body on the configured side, runs the script, and then updates the Content-Length header. Server-sent event (SSE) bodies, which have the text/event-stream content type, aren’t buffered. The policy transforms SSE bodies one event at a time and removes the Content-Length header instead of updating it.

Buffered payloads count toward the Omni Gateway payload limit, which is 1 MB by default. To change the limit on a self-managed Omni Gateway, set the FLEX_DOWNSTREAM_CONNECTION_BUFFER_LIMIT_BYTES environment variable. In Connected Mode, you can also set Global connection buffer limit in Runtime Manager. For more information, see Configure Global and Routing Settings in Runtime Manager. For more information about the limit, see Omni Gateway Limits and Performance Metrics.

Configure Policy Parameters

Omni Gateway Local Mode

When you apply the policy via declarative configuration files, Refer to the following policy definition and table of parameters:

- policyRef:
    name: dataweave-body-transformation-flex
  config:
    script: <string>        // REQUIRED - DataWeave script
    requestFlow: <string>   // REQUIRED - "onRequest" or "onResponse"
Parameter Required Default Value Description

script

Required

—

A DataWeave script that transforms the body. The script receives attributes.headers and payload. Declare the output MIME type in the script when needed, such as output application/json.

requestFlow

Required

—

Whether the policy transforms the request or response body. Supported values: onRequest or onResponse.

Resource Configuration Example

This example transforms the request payload to a new structure that includes the value from the custom header custom-client-header and fields from the payload:

  policies:
    - policyRef:
        name: dataweave-body-transformation-flex
      config:
        script: |
          {
            "id": attributes.headers.'custom-client-header',
            "username": payload.name ++ payload.surname
          }

To transform the response body and set a custom output format such as XML, declare the output and use requestFlow: onResponse:

  policies:
    - policyRef:
        name: dataweave-body-transformation-flex
      config:
        script: |
          output application/xml
          ---
          {
            "trace": payload.headers.'X-Amzn-Trace-Id',
            "custom-data": "custom-value"
          }
        requestFlow: onResponse

Managed Omni Gateway and Omni Gateway Connected Mode

When you apply the policy from the UI, the following parameters are displayed:

Parameter Description

Policy type

DataWeave Body Transformation

Script

The DataWeave script that transforms the body

Request flow

Whether the policy transforms the request or response body