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.
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 |
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"
}
{
"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. |
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>