Skip to content

gravitee-io/gravitee-policy-assign-content

Repository files navigation

Assign content policy

Gravitee.io License Releases CircleCI

Phase

onRequest onResponse onRequestContent onResponseContent

-

-

X

X

Description

You can use the assign-content policy to change or transform the content of the request/response body or message.

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.

Compatibility with APIM

Plugin version APIM version

Up to 1.6.x

Up to 3.9.x

1.7.x

3.10.x to 3.20.x

2.x

4.0 to latest

Configuration

Property Required Description Type Default

scope

X

The execution scope of the policy.

scope

REQUEST

body

X

The data to push as request or response body content.

string

-

Sample
"policy-assign-content": {
    "scope":"REQUEST",
    "body":"Put your content here"
}

Examples

Inject a dictionary value and the application into the request payload

{
  "example": "${context.dictionaries['my-dictionary']['my-value']}",
  "application": "${context.attributes['application']}"
}
Tip
You can find more information about default attributes in the Expression Language documentation in the API Publisher Guide.

Incoming request body content

{
  "symbol": "EUR"
}

Policy example to transform from JSON to XML

Input:

<#assign body = request.content?eval >
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:GetQuote>
         ${body.symbol}
      </web:GetQuote>
   </soapenv:Body>
</soapenv:Envelope>

Expected output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">
 <soapenv:Header/>
 <soapenv:Body>
 <web:GetQuote>
 EUR
 </web:GetQuote>
 </soapenv:Body>
</soapenv:Envelope>

Errors

HTTP status code

Code Message

500

The body content cannot be transformed.