description |
---|
This page provides the technical details of the Assign Content policy |
You can use the assign-content
policy to change or transform the content of the request body or response body.
This policy is compatible with the Freemarker template engine, which allows you to apply complex transformations, such as transforming from XML to JSON and vice versa.
By default, you can access multiple objects from the template context: request and response bodies, dictionaries, context attributes and more.
Functional and implementation information for the assign-content
policy is organized into the following sections:
{% hint style="warning" %} This policy can be applied to v2 APIs, v4 HTTP proxy APIs, and v4 message APIs. It cannot be applied to v4 TCP proxy APIs. {% endhint %}
{% tabs %} {% tab title="HTTP proxy API example" %} You could use the Assign Content policy to inject a dictionary value and application into the request payload:
{
"example": "${context.dictionaries['my-dictionary']['my-value']}",
"application": "${context.attributes['application']}"
}
{% endtab %}
{% tab title="Message API example" %} You could use the Assign Content policy to inject a dictionary value and metadata into the message:
{
"example": "${message.dictionaries['my-dictionary']['my-value']}",
"metadata": "${message.attributes['metadata']}"
}
{% endtab %} {% endtabs %}
Sample policy configuration:
{% code title="Sample Configuration" %}
"policy-assign-content": {
"scope":"REQUEST",
"body":"Put your content here"
}
{% endcode %}
The phases checked below are supported by the assign-content
policy:
v2 Phases | Compatible? | v4 Phases | Compatible? |
---|---|---|---|
onRequest | false | onRequest | false |
onResponse | false | onResponse | false |
onRequestContent | true | onMessageRequest | true |
onResponseContent | true | onMessageResponse | true |
You can configure the assign-content
policy with the following options:
Property | Required | Description | Type | Default |
---|---|---|---|---|
scope | true | The execution scope of the policy | scope | REQUEST |
body | true | The data to push as request or response body content | string | - |
The following is the compatibility matrix for APIM and the assign-content
policy:
Plugin Version | Supported APIM versions |
---|---|
Up to 1.6.x | Up to 3.9.x |
1.7.x | 3.10.x to 3.20.x |
2.x | 4.0+ |
Phase | HTTP status code | Error template key |
---|---|---|
onRequestContent | 500 | The body content cannot be transformed. |
onResponseContent | 500 | The body content cannot be transformed. |
onMessageRequest | 400 | The body content cannot be transformed. |
onMessageResponse | 500 | The body content cannot be transformed. |
{% @github-files/github-code-block url="https://github.com/gravitee-io/gravitee-policy-assign-content/blob/master/CHANGELOG.md" %}