description |
---|
This page provides the technical details of the Assign Attributes policy |
You can use the assign-attributes
policy to set variables such as request attributes and other execution context attributes.
You can use it to retrieve initial request attributes after Transform headers
or Transform query parameters
policies and reuse them in other policies (Dynamic routing
, for example).
Functional and implementation information for the assign-attributes
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" %} To inject an attribute that will dynamically determine if the content is in JSON format:
"assign-attributes": {
"attributes": [
{
"name": "isJson,
"value": "'application/json'.equals(#request.headers['Content-Type'])"
}
]
}
To extract the request attribute and get the format of the content you can use the following syntax:
{#context.attributes['isJson']}
Request objects
You can also be more general and inject complex objects into the context attributes:
"assign-attributes": {
"attributes": [
{
"name": "initialRequest,
"value": "{#request}"
}
]
}
To extract request attributes and get the Content-Type header you can use the following syntax:
{#context.attributes['initialRequest'].headers['Content-Type']}
{% endtab %}
{% tab title="Message API example" %} To inject an attribute that will dynamically determine if the content is in JSON format:
"assign-attributes": {
"attributes": [
{
"name": "isJson,
"value": "'application/json'.equals(#message.headers['Content-Type'])"
}
]
}
To extract the message attribute and get the format of the content you can use the following syntax:
{#message.attributes['isJson']}
Message objects
You can also be more general and inject complex objects into the message attributes:
"assign-attributes": {
"attributes": [
{
"name": "initialMessage,
"value": "{#message}"
}
]
}
To extract message attributes and get the Content-Type header you can use the following syntax:
{#message.attributes['initialMessage'].headers['Content-Type']}
To assign an attribute to the content of a message:
"assign-attributes": {
"attributes": [
{
"name": "messageContent,
"value": "{#message.content}"
}
]
}
{% endtab %} {% endtabs %}
The phases checked below are supported by the assign-attributes
policy:
v2 Phases | Compatible? | v4 Phases | Compatible? |
---|---|---|---|
onRequest | true | onRequest | true |
onResponse | true | onResponse | true |
onRequestContent | true | onMessageRequest | true |
onResponseContent | true | onMessageResponse | true |
You can configure the assign-attributes
policy with the following options:
Property | Required | Description | Type | Default |
---|---|---|---|---|
scope | only for v4 proxy APIs | The execution scope (request or response ) | string | REQUEST |
attributes | X | List of attributes | See table below |
You can configure the assign-attributes
policy with the following attributes:
Property | Required | Description | Type | Default |
---|---|---|---|---|
name | X | Attribute name | string | |
value | X | Attribute value (can be EL) | string |
The following is the compatibility matrix for APIM and the assign-attributes
policy:
Plugin Version | Supported APIM versions |
---|---|
Up to 1.x | All |
From 2.x | 4.0+ |
Phase | HTTP status code | Error template key |
---|---|---|
onRequest | 500 | An error occurred while setting request attributes in the execution context |
onResponse | 500 | An error occurred while setting request attributes in the execution context |
onRequestContent | 500 | An error occurred while setting request attributes in the execution context |
onResponseContent | 500 | An error occurred while setting request attributes in the execution context |
onMessageRequest | 500 | An error occurred while setting request attributes in the execution context |
onMessageResponse | 500 | An error occurred while setting request attributes in the execution context |
{% @github-files/github-code-block url="https://github.com/gravitee-io/gravitee-policy-assign-attributes/blob/master/CHANGELOG.md" %}