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 Flex Gateway version available

v1.12.0

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. The gateway streams body processing. 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. In the request or response, the gateway processes the body in streaming mode and updates Content-Type and Content-Length after transformation.

Configure Policy Parameters

Flex Gateway Local Mode

In Local Mode, you apply the policy to your API 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 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 Body Transformation

Script

The DataWeave script that transforms the body

Request flow

Whether the policy transforms the request or response body

Method and Resource conditions

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