Contact Us 1-800-596-4880

HTTP Policy Transform Extension

In Mule version 4.1.0 and later, the HTTP Policy Transform Extension is available for use in policies. This extension simplifies the modification of HTTP requests and responses that go through the different policies. This documentation applies to HTTP Policy Transform Extension 3.0.0 and later.

Adding the extension as a Maven dependency to a policy

To use the HTTP Policy Transform Extension in a policy, first you must add the Maven dependency to the POM file of the policy. Add the following code in the dependencies section of policy’s POM file:

  <dependency>
      <groupId>com.mulesoft.anypoint</groupId>
      <artifactId>mule-http-policy-transform-extension</artifactId>
      <version>${httpPolicyTransformVersion}</version>
      <classifier>mule-plugin</classifier>
      <exclusions>
          <exclusion>
              <groupId>org.mule.connectors</groupId>
              <artifactId>mule-http-connector</artifactId>
          </exclusion>
      </exclusions>
  </dependency>

NOTE: When a policy using the HTTP Policy Transform Extension is packaged, the Mule HTTP Connector is unnecessarily added to the policy JAR file. The connector is already present in the application to which the policy is applied. For that reason, the exclusion is added in the above snippet.

Additionally, ensure that you set the mule-maven-plugin parameter version to 3.3.2 or later in the POM file of the policy.

Namespace

<mule
xmlns:http-transform="http://www.mulesoft.org/schema/mule/http-policy-transform"

xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http-policy-transform http://www.mulesoft.org/schema/mule/http-policy-transform/current/mule-http-policy-transform.xsd">

Operations

  • Add Headers Operations

    • Add Request Headers

    • Add Request Headers List (Since mule-http-policy-transform-extension 3.1.0)

    • Add Response Headers

    • Add Response Headers List (Since mule-http-policy-transform-extension 3.1.0)

  • Remove Headers

  • Set Response

  • Set Request

Add Headers Operations

Add Request Headers

This operation is used to inject headers in the request. To use this operation, you must initiate a call from:

  • Source Policy, before the execute-next is invoked.

  • Operation Policy, before the execute-next is invoked.

Parameters
  • headers

    A DataWeave expression that resolves to the Map of the headers to be added to present message attributes.

    Example

    <http-transform:add-request-headers>
    <http-transform:headers>#[
    {
      'FirstHeader': 'FirstHeaderValue',
      'SecondHeader': 'SecondHeaderValue',
      'ThirdHeader': 'ThirdHeaderValue'
    }
    ]</http-transform:headers>
    </http-transform:add-request-headers>

Add Request Headers List

This operation was added in mule-http-policy-transform-extension 3.1.0.

This operation is used to inject headers in the request. To use this operation, you must initiate a call from:

  • Source Policy, before execute-next is invoked.

  • Operation Policy, before execute-next is invoked.

Parameters
  • http-transform:new-headers

    An http-transform:header must be added for each header. Each http-transform:header must have the headerName and headerValue attributes defined with a literal or a DataWeave expression.

    Example

    <http-transform:add-request-headers-list>
        <http-transform:new-headers>
            <http-transform:header headerName="FirstHeader" headerValue="FirstHeaderValue"/>
            <http-transform:header headerName="#['SecondHeader']" headerValue="#['SecondHeaderValue']"/>
            <http-transform:header headerName="ThirdHeader" headerValue="ThirdHeaderValue"/>
        </http-transform:new-headers>
    </http-transform:add-request-headers-list>

The operation allows DataWeave expressions when the policy configuration has a property type of keyvalues with multiple values.

Example

...
configuration:
  - propertyName: requestHeaders
    name: Reques Headers
    description: Header Names and values
    type: keyvalues
    optional: true
    allowMultiple: true
...

With the above configuration shown, you can use handlebars to expand the headers to be added in the policy:

...
<http-transform:add-request-headers-list>
    <http-transform:new-headers>
        {{#each requestHeaders}}
        <http-transform:header headerName="{{{this.key}}}" headerValue="{{{this.value}}}"/>
        {{/each}}
    </http-transform:new-headers>
</http-transform:add-request-headers-list>
...

Add Response Headers

This operation is used to inject headers in the response. To use this operation, you must initiate a call from:

  • Source Policy, after the execute-next is invoked.

  • Operation Policy, after the execute-next is invoked.

Parameters
  • headers

    A DataWeave expression that resolves to the Map of headers to be added to present message attributes.

    Example

    <http-transform:add-response-headers>
    <http-transform:headers>#[
    {
      'FirstHeader': 'FirstHeaderValue',
      'SecondHeader': 'SecondHeaderValue',
      'ThirdHeader': 'ThirdHeaderValue'
    }
    ]</http-transform:headers>
    </http-transform:add-response-headers>

Add Response Headers List

This operation was added in mule-http-policy-transform-extension 3.1.0.

This operation is used to inject headers in the response. To use this operation, you must initiate a call from:

  • Source Policy, after the execute-next is invoked.

  • Operation Policy, after the execute-next is invoked.

Parameters
  • http-transform:new-headers

    An http-transform:header must be added for each header. Each http-transform:header must have the headerName and headerValue attributes defined with a literal or a DataWeave expression.

    Example

    <http-transform:add-response-headers-list>
        <http-transform:new-headers>
            <http-transform:header headerName="FirstHeader" headerValue="FirstHeaderValue"/>
            <http-transform:header headerName="#['SecondHeader']" headerValue="#['SecondHeaderValue']"/>
            <http-transform:header headerName="ThirdHeader" headerValue="ThirdHeaderValue"/>
        </http-transform:new-headers>
    </http-transform:add-response-headers-list>

The operation allows DataWeave expressions when the policy configuration has a property of type keyvalues with multiple values.

Example

...
configuration:
  - propertyName: responseHeaders
    name: Reques Headers
    description: Header Names and values
    type: keyvalues
    optional: true
    allowMultiple: true
...

With the above configuration shown, you can use handlebars to expand the headers to be added in the policy:

...
<http-transform:add-reponse-headers-list>
    <http-transform:new-headers>
        {{#each responseHeaders}}
        <http-transform:header headerName="{{{this.key}}}" headerValue="{{{this.value}}}"/>
        {{/each}}
    </http-transform:new-headers>
</http-transform:add-response-headers-list>
...

Remove Headers

This operation removes the headers from both requests and responses of the HTTP Listener and Requester. The returned attributes are of the same type as that of the input attributes. This operation is case-insensitive.

Parameters

  • header-names

    A Data-Weave expression that resolves to an array of header names to be removed.

    Example

    <http-transform:remove-headers>
        <http-transform:header-names>#[['FirstHeader', 'SecondHeader']]</http-transform:header-names>
    </http-transform:remove-headers>

Set Response

This operation sets a complete HTTP response. You use this operation when the policy prevents the flow from being executed, thereby resulting in no HTTP response being set.

Parameters

  • statusCode-- An integer representing the HTTP status code of the response.

  • reasonPhrase-- A string representing the HTTP reason phrase of the response.

  • body-- The content of the HTTP response.

  • headers-- A DataWeave expression that resolves to the map of headers of the HTTP response

    Example

    <http-transform:set-response statusCode="203" reasonPhrase="policyReasonPhrase">
        <http-transform:body>#['policyPayload']</http-transform:body>
        <http-transform:headers>#[
            {
                'FirstHeader': 'FirstHeaderValue',
                'SecondHeader': 'SecondHeaderValue',
                'ThirdHeader': 'ThirdHeaderValue'
            }
        ]</http-transform:headers>
    </http-transform:set-response>

Set Request

This operation sets a complete HTTP request that the HTTP Requester executes.

Parameters

  • requestPath: The path where the HTTP Request is being sent.

  • body: The content of the HTTP Request.

  • headers: A DataWeave expression that resolves to the map of headers of the HTTP request.

  • uriParams: A DataWeave expression that resolves to the map of URI params of the HTTP request.

  • queryParams: A DataWeave expression that resolves to the map of query params of the HTTP request.

    Example

    <http-transform:set-requester-request requestPath="/backend/policy/{policyUriParam}">
        <http-transform:body>#['policyPayload']</http-transform:body>
        <http-transform:headers>#[
            {
                'FirstHeader': 'FirstHeaderValue',
                'SecondHeader': 'SecondHeaderValue',
                'ThirdHeader': 'ThirdHeaderValue'
            }
        ]</http-transform:headers>
        <http-transform:uri-params>#[
            {
                'FirstUriParam': 'FirstUriParamValue',
                'SecondUriParam': 'SecondUriParamValue'
            }
        ]</http-transform:uri-params>
        <http-transform:query-params>#[
            {
                'FirstQueryParam': 'FirstQueryParamValue',
                'SecondQueryParam': 'SecondQueryParamValue'
            }
        ]</http-transform:query-params>
    </http-transform:set-requester-request>