News New Look, Same Vision: KrakenD’s Website Redesign

You are viewing a previous version of KrakenD Enterprise Edition (v2.1), go to the latest version

Document updated on Oct 25, 2022

Response manipulation with regular expressions

The content replacer plugin allows you to modify the response of your services by doing literal replacements or more sophisticated replacements with regular expressions.

The plugin works both in the endpoint and the backend sections. Depending on what stage of the pipe you want to modify, the content is replaced before it gets to the user (endpoint) or before the merging operation (backend).

The content to replace must be a string and located inside an object or a children object. You cannot replace objects, arrays, or any other type, or access elements inside arrays.

{
    "extra_config": {
        "plugin/req-resp-modifier": {
            "name": [
                "content-replacer"
            ],
            "content-replacer": {
                "data.credit_card": {
                    "@comment": "Ridiculous card masking. Take 4 digits and remove the rest. Credit card is inside a data object.",
                    "find": "(^\\d{4})(.*)",
                    "replace": "${1}-XXXX",
                    "regexp": true
                },
                "message": {
                    "@comment": "Replace '6 items left' with '6' on the message field",
                    "find": " items left",
                    "replace": ""
                }
            }
        }
    }
}
Fields of "plugin/content-replacer": { "additionalProperties":{} }
* required fields

find * string
The find expression or literal you want to use.
regexp boolean
When you are passing regular expressions instead of literal values, set it to true.
Defaults to false
replace * string
The literal string or expression you want to use as a replacement.

The configuration of the plugin uses as keys the objects of the response. The dot . notation is reserved to access nested elements and you cannot use fields containing dots as part of the key name with this plugin. In the example above, the data.credit_card represents a nested object, like:

{
	"data": {
		"credit_card": "1234-56789-1234-5678"
	}
}
Scarf

Unresolved issues?

The documentation is only a piece of the help you can get! Whether you are looking for Open Source or Enterprise support, see more support channels that can help you.

See all support channels