docs.intersystems.com
API Management Documentation
InterSystems: The power behind what matters

Response Transformer Advanced

Terminology

Configuration

Enabling the plugin on a Service

With a database

Configure this plugin on a Service by making the following request:

$ curl -X POST http://localhost:8001/services/{service}/plugins \
    --data "name=response-transformer-advanced"  \
    --data "config.remove.headers=x-toremove, x-another-one" \
    --data "config.remove.json=json-key-toremove, another-json-key" \
    --data "config.add.headers=x-new-header:value,x-another-header:something" \
    --data "config.add.json=new-json-key:some_value, another-json-key:some_value" \
    --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value"

Without a database

Configure this plugin on a Service by adding this section do your declarative configuration file:

plugins:
- name: response-transformer-advanced
  service: {service}
  config: 
    remove.headers: x-toremove, x-another-one
    remove.json: json-key-toremove, another-json-key
    add.headers: x-new-header:value,x-another-header:something
    add.json: new-json-key:some_value, another-json-key:some_value
    append.headers: x-existing-header:some_value, x-another-header:some_value

In both cases, {service} is the id or name of the Service that this plugin configuration will target.

Enabling the plugin on a Route

With a database

Configure this plugin on a Route with:

$ curl -X POST http://localhost:8001/routes/{route}/plugins \
    --data "name=response-transformer-advanced"  \
    --data "config.remove.headers=x-toremove, x-another-one" \
    --data "config.remove.json=json-key-toremove, another-json-key" \
    --data "config.add.headers=x-new-header:value,x-another-header:something" \
    --data "config.add.json=new-json-key:some_value, another-json-key:some_value" \
    --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value"

Without a database

Configure this plugin on a Route by adding this section do your declarative configuration file:

plugins:
- name: response-transformer-advanced
  route: {route}
  config: 
    remove.headers: x-toremove, x-another-one
    remove.json: json-key-toremove, another-json-key
    add.headers: x-new-header:value,x-another-header:something
    add.json: new-json-key:some_value, another-json-key:some_value
    append.headers: x-existing-header:some_value, x-another-header:some_value

In both cases, {route} is the id or name of the Route that this plugin configuration will target.

Enabling the plugin on a Consumer

With a database

You can use the http://localhost:8001/plugins endpoint to enable this plugin on specific Consumers:

$ curl -X POST http://localhost:8001/consumers/{consumer}/plugins \
    --data "name=response-transformer-advanced" \
     \
    --data "config.remove.headers=x-toremove, x-another-one" \
    --data "config.remove.json=json-key-toremove, another-json-key" \
    --data "config.add.headers=x-new-header:value,x-another-header:something" \
    --data "config.add.json=new-json-key:some_value, another-json-key:some_value" \
    --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value"

Without a database

Configure this plugin on a Consumer by adding this section do your declarative configuration file:

plugins:
- name: response-transformer-advanced
  consumer: {consumer}
  config: 
    remove.headers: x-toremove, x-another-one
    remove.json: json-key-toremove, another-json-key
    add.headers: x-new-header:value,x-another-header:something
    add.json: new-json-key:some_value, another-json-key:some_value
    append.headers: x-existing-header:some_value, x-another-header:some_value

In both cases, {consumer} is the id or username of the Consumer that this plugin configuration will target.

You can combine consumer_id and service_id

in the same request, to furthermore narrow the scope of the plugin.

Global plugins

A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of IAM) is considered "global", and will be run on every request. Read the Plugin Reference and the Plugin Precedence sections for more information.

Parameters

Here's a list of all the parameters which can be used in this plugin's configuration:

form parameterdescription
nameThe name of the plugin to use, in this case response-transformer-advanced
service_idThe id of the Service which this plugin will target.
route_idThe id of the Route which this plugin will target.
enabled

default value: true
Whether this plugin will be applied.
consumer_idThe id of the Consumer which this plugin will target.
config.remove.headers
optional

List of header names. Unset the header(s) with the given name.

config.remove.json
optional

List of property names. Remove the property from the JSON body if it is present.

config.remove.if_status
optional

List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes.

config.replace.headers
optional

List of headername:value pairs. If and only if the header is already set, replace its old value with the new one. Ignored if the header is not already set.

config.replace.json
optional

List of property:value pairs. If and only if the parameter is already present, replace its old value with the new one. Ignored if the parameter is not already present.

config.replace.body
optional

String with which to replace the entire response body

config.replace.if_status
optional

List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes

config.add.headers
optional

List of headername:value pairs. If and only if the header is not already set, set a new header with the given value. Ignored if the header is already set.

config.add.json
optional

List of property:value pairs. If and only if the property is not present, add a new property with the given value to the JSON body. Ignored if the property is already present.

config.add.if_status
optional

List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes

config.append.headers
optional

List of headername:value pairs. If the header is not set, set it with the given value. If it is already set, a new header with the same name and the new value will be set.

config.append.json
optional

List of property:value pairs. If the property is not present in the JSON body, add it with the given value. If it is already present, the two values (old and new) will be aggregated in an array.

config.append.if_status
optional

List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes

Note: if the value contains a , then the comma separated format for lists cannot be used. The array notation must be used instead.

Order of execution

Plugin performs the response transformation in following order

remove ?> replace ?> add ?> append

Examples

In these examples we have the plugin enabled on a Route. This would work similar for Services.

$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
  --data "name=response-transformer-advanced" \
  --data "config.add.headers[1]=h1:v1" \
  --data "config.add.headers[2]=h2:v1"
upstream response headers proxied response headers
h1: v1
  • h1: v1
  • h2: v1
$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
  --data "name=response-transformer-advanced" \
  --data "config.add.headers=h1:v1,h2:v2"
upstream response headers proxied response headers
h1: v1
  • h1: v1
  • h2: v1
$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
  --header 'content-type: application/json' \
  --data '{"name": "response-transformer-advanced", "config": {"add": {"headers": ["h1:v2", "h2:v1"]}}}'
upstream response headers proxied response headers
h1: v1
  • h1: v1
  • h2: v1
$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
  --data "name=response-transformer-advanced" \
  --data "config.add.json=p1:v1,p2=v2" \
  --data "config.add.headers=h1:v1"
upstream response headers proxied response headers
h1: v2
  • h1: v2
  • h2: v1
h3: v1
  • h1: v1
  • h2: v1
  • h3: v1
upstream response JSON body proxied response body
{} {?p1? : ?v1?, ?p2?: ?v2?}
{?p1? : ?v2?} {?p1? : ?v2?, ?p2?: ?v2?}
$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
  --header 'content-type: application/json' \
  --data '{"name": "response-transformer-advanced", "config": {"append": {"headers": ["h1:v2", "h2:v1"]}, "remove": {"json": ["p1"]}}}'
upstream response headers proxied response headers
h1: v1
  • h1: v1
  • h1: v2
  • h2: v1
upstream response JSON body proxied response body
{?p2?: ?v2?} {?p2?: ?v2?}
{?p1? : ?v1?, ?p2? : ?v1?} {?p2?: ?v2?}
$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
  --data "name=response-transformer-advanced" \
  --data "config.replace.body='{\"error\": \"internal server error\"}'" \
  --data "config.replace.if_status=500"

Note: the plugin doesn?t validate the value in config.replace.body against the content type as defined in the Content-Type response header.