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