Skip to content

gravitee-io/gravitee-policy-assign-attributes

Repository files navigation

Assign attributes policy

Gravitee.io License Releases CircleCI

Phases

V3 engine

onRequest onResponse onRequestContent onResponseContent

X

X

X

X

V4 engine

onRequest onResponse onMessageRequest onMessageResponse

X

X

X

X

Description

You can use the assign-attributes policy to set variables such as request attributes, message attributes and other execution context attributes.

When you are using this policy on the message request or response, the attribute will be available in the message attribute list and not on the context one.

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).

Compatibility with APIM

Plugin version APIM version

Up to 1.x

All

From 2.x

4.0+

Configuration

You can configure the policy with the following options:

Table 1. Configuration
Property Required Description Type Default

scope

only for v3 engine

The execution scope (request or response)

string

REQUEST

attributes

X

List of attributes

See Attribute

Table 2. Attribute
Property Required Description Type Default

name

X

attribute name

string

value

X

attribute value (can be EL)

string

Examples

Request header

Let’s say we want to inject request attributes into the context attributes.

"assign-attributes": {
    "attributes": [
        {
            "name": "initialContentTypeHeader,
            "value": "{#request.headers['Content-Type']}"
        },
        {
            "name": "initialFooParamHeader,
            "value": "{#request.params['foo']}"
        }
    ]
}

To extract the request attributes you can use the following syntax:

Get the content-type header:

{#context.attributes['initialContentTypeHeader']}

Get the foo query param:

{#context.attributes['initialFooParamHeader']}

Request objects

You can also be more general and put complex objects into the context attributes:

"assign-attributes": {
    "attributes": [
        {
            "name": "initialRequest,
            "value": "{#request}"
        }
    ]
}

To extract the request attributes you can use the following syntax:

Get the content-type header:

{#context.attributes['initialRequest'].headers['content-type']}

Get the foo query param:

{#context.attributes['initialRequest'].params['foo']}

Message

You can use for example the content of a message:

"assign-attributes": {
    "attributes": [
        {
            "name": "messageContent,
            "value": "{#message.content}"
        }
    ]
}

Errors

HTTP status code

Code Message

500

An error occurred while setting request attributes in the execution context