Contact Us 1-800-596-4880

DataWeave Headers Transformation Outbound Policy

Policy name

DataWeave Headers Transformation Outbound

Summary

Transforms the headers of request or response traffic for a specific upstream service with a DataWeave script

Category

Transformation

First Omni Gateway version available

v1.15.0

Release Notes

DataWeave Headers Transformation Outbound

Returned Status Codes

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

Summary

The DataWeave Headers Transformation Outbound policy transforms the headers of request or response traffic for a specific upstream service with a DataWeave script. It works the same way as the DataWeave Headers Transformation policy, but you apply it to an upstream service, so it transforms only the traffic for that upstream service. It can change the headers of requests that Omni Gateway sends to the upstream service, or the headers of responses that Omni Gateway receives from it.

Use this policy when you want to:

  • Add headers that an upstream service requires, so that clients don’t need to send them.

  • Rename or remove headers before a request reaches the upstream service, so that the upstream service receives only the headers it expects.

  • Set headers on an upstream response, based on values from the response body, so that the client can read them without parsing the body.

To change the body instead of the headers, use the DataWeave Body Transformation Outbound policy. To transform traffic for all upstream services at once, use the DataWeave Headers Transformation policy.

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:

apiVersion: gateway.mulesoft.com/v1beta1
kind: PolicyBinding
metadata:
  name: dataweave-headers-transformation-outbound
spec:
  targetRef:
    kind: Service
    name: upstream
  policyRef:
    name: dataweave-headers-transformation-outbound-flex
  config:
    script: <string>           // REQUIRED - DataWeave script
    requestFlow: <string>      // REQUIRED - "onRequest" or "onResponse"
    requiresPayload: <boolean> // REQUIRED
Parameter Required or Optional Default Value Description

script

Required

—

A DataWeave script that returns a map of header names to values. The script receives attributes.headers. If requiresPayload is true, the script also receives the message body in the payload binding.

requestFlow

Required

—

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

requiresPayload

Required

false

If true, the script receives the message body in the payload binding, and Omni Gateway buffers the body for the transformation. Enabling this setting can reduce performance.

Managed Omni Gateway and Omni Gateway Connected Mode

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

Basic Configuration

Element Required Description

Script

Yes

The DataWeave script that returns a map of header names to values. The script receives attributes.headers. To test your script, use the DataWeave Playground.

Requires payload

Yes

Whether the script requires the message body. Enabling this setting can reduce performance.

Request flow

Yes

Whether the policy transforms the request or the response headers.

How This Policy Works

The policy transforms the headers of the traffic on the side that Request flow (requestFlow) sets. Set it to On request (onRequest) to transform the request that Omni Gateway sends to the upstream service. Set it to On response (onResponse) to transform the response that Omni Gateway receives from the upstream service. The other side passes through unchanged.

On the configured side, Omni Gateway removes the existing headers and runs the script. The script must return a map. Each entry in the map becomes a header. For a header with multiple values, use a list of values.

Payload Handling

How the policy handles the body on the configured side depends on Requires payload (requiresPayload):

  • false: The policy reads headers only and doesn’t buffer the body.

  • true: The policy buffers the full body so that the script can read payload.

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.

To avoid buffering, set requiresPayload to false and write the script to use only attributes.headers.

Example Configurations

The following examples demonstrate common configuration patterns for the DataWeave Headers Transformation Outbound policy.

Add Headers Conditionally

This example sets headers on the request that Omni Gateway sends to the upstream service. When the custom-client-header header is fixed_client, the script replaces it with custom-123123231231231231. Otherwise, the script uses the header value, or client-id-not-set if the header is missing:

apiVersion: gateway.mulesoft.com/v1beta1
kind: PolicyBinding
metadata:
  name: dataweave-headers-transformation-outbound
spec:
  targetRef:
    kind: Service
    name: upstream
  policyRef:
    name: dataweave-headers-transformation-outbound-flex
  config:
    requestFlow: onRequest
    requiresPayload: false
    script: |
      {
        "custom_client_secret": "fixed_secret_123123231231231231",
        "custom_client_id": if (attributes.headers.'custom-client-header'?)
          attributes.headers.'custom-client-header' match {
            case literalMatch: "fixed_client" -> "custom-123123231231231231"
            else -> attributes.headers.'custom-client-header'
          }
        else "client-id-not-set"
      }

Add Headers from the Payload

This example appends headers that use values from the payload. Because the script reads payload, requiresPayload is true:

apiVersion: gateway.mulesoft.com/v1beta1
kind: PolicyBinding
metadata:
  name: dataweave-headers-transformation-outbound
spec:
  targetRef:
    kind: Service
    name: upstream
  policyRef:
    name: dataweave-headers-transformation-outbound-flex
  config:
    requestFlow: onRequest
    requiresPayload: true
    script: |
      attributes.headers ++ {
        "custom_client_id_from_payload": payload.id,
        "custom_client_secret_from_payload": payload.secret
      }